1

I am following a tutorial in which they use BIO_printf(bio_out,"%02x",bs->data[i] ); in order to get the characters of a serial number stored in bs->data (which is an array of unsigned char) and they use "%02x" to specify the format of the char. I am using c++ and I want to add each of these chars to a stringstream but I can't find a way to translate the format and instead of a serial number I get ጄ냾�㮚㭖嵺ﭔ촋ᙰ.

I have tried using

std::stringstream serial("Serial: ");
for (int i = 0; i < bs->length; i++) 
{
    serial << std::setw(2) << std::hex << bs->data[i] << std::endl;
}

but I still don't get a valid string, and

char* buffer = const_cast<char*>(serial.str().c_str());
sprintf(buffer, "%02x", bs->data[i]);

doesn't seem to work either (don't mind the const cast, I know it is bad practice)

  • Please show us the stream-based code that didn't work. I think I know what might be wrong, but I can't comment on code I can't see. – Paul Sanders May 31 '22 at 09:35
  • Does htis answer your question? https://stackoverflow.com/questions/5100718/integer-to-hex-string-in-c – KamilCuk May 31 '22 at 09:38
  • @PaulSanders I hope the edited version explains better my aproach – user18743773 May 31 '22 at 09:43
  • Where in your for loop do you actually access `bs->data[i]`? – Botje May 31 '22 at 09:44
  • @Botje I'm sorry, I forgot to add it in the edit – user18743773 May 31 '22 at 09:45
  • Where do you **use** serial? Since you say it prints something with weird characters. – Botje May 31 '22 at 09:48
  • I am using it just to know if the output of the reading is correct. So I am logging it to the console later in the code @Botje – user18743773 May 31 '22 at 09:50
  • *how* are you logging it to the console? – Botje May 31 '22 at 09:50
  • I am using Unreal Engine, so `UE_LOG(LogTemp, Warning, TEXT("%s"), serial.str().c_str());`. Edit: Okay, I just realized about the problem, I am printing it as an array of chars ("%s") so no matter how I format it, it will be interpreted as an array of chars. Thanks @Botje for the clues :) – user18743773 May 31 '22 at 09:51
  • You forgot to include that very important bit of context. Because [UE_LOG wants a `TCHAR*` instead of `char*`](https://stackoverflow.com/questions/41407427/inlining-the-unrealengine-ue-log-macro-with-c). If you just pass a `char*` it will interpret each two bytes as a composite 16-bit character, which leads to tears. In summary: your way of constructing the hex string was just fine, but your way of printing it was flawed. – Botje May 31 '22 at 09:57

1 Answers1

2

You can use a combination of the std::setw(), std::setfill() and std::hex manipulators to set the width and fill characters. Also, you need to explicitly cast a char variable to an int in order to get the numerical representation, otherwise the << operator (on a char) will output (or attempt to output) the represented character.

Here's a short example using the std::cout stream (though the manipulators work in the same way on a std::stringstream object):

#include <iostream>
#include <iomanip>

int main()
{
    char c = 13;
    std::cout << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(c) << std::endl; // Shows 0d
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83