today I tried to convert a hex string to an unsigned char[]
string test = "fe5f0c";
unsigned char* uchar= (unsigned char *)test.c_str();
cout << uchar << endl;
This resulted in the output of
fe5f0c
hrmpf :-(. The desired behaviour would be as follows:
unsigned char caTest[2];
caTest[0] = (unsigned char)0xfe;
caTest[1] = (unsigned char)0x5f;
caTest[2] = (unsigned char)0x0c;
cout << caTest << endl;
which prints unreadable ascii code. As so often I am doing something wrong ^^. Would appreciate any suggestions.
Thanks in advance
Sure, you just have to isolate the bits you are interested in after parsing:
Note that I changed the input string to an eight digit number, since otherwise the array would start with the value 0, and
operator<<would interpret that as the end and you wouldn’t be able to see anything.