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
unsigned char caTest[4];and forgotcaTest[3] = 0;, but still it's not clear what you want. It looks like you want to send the ASCII representations of hex-pair values to stdout, regardless of whether they're printable.caTestas a string. Strings are null-terminated. If you don't do this, then you'll end up reading past the end of the buffer.