How do I format the values of an array of `unsigned char` in hexadecimal?

shivesh suman

I have an array of unsigned char which contains hex values. I would like to show it in the logs and in order to do it I need to convert it into a string.

Obviously if I directly try to use the character array as a string I get gibberish as is it interpreted as ASCII (e.g., it will be rendered as: "\xfê)Z+’\x2". Ideally I want to show the raw hex values without them being interpreted.

I have tried to copy it into a string but have not been successful. I am going to present what I have tried and the results that I got:

//My starting data type is unsigned char *

unsigned char* index = Block_data.Index;

//I convert it to const char* since most functions I could find for this type of
//conversion require a const char *, if this step can be avoided: I will do that

const char* indexc1 = reinterpret_cast<const char*>(index);

char outVal[9]; //allocate target buffer space

sprintf_s(outVal, "%9x", indexc1); //copy to target. This manages to copy the 
                                 // address of indexc1 and not its contents
sprintf_s(outVal, "%9x", *indexc1); //This gets one character from the 
                                  // address

sscanf_s(indexc1, "%9x", outVal); //This gets empty string

I feel that the answer may be quite simple but after some research I tried the above and have not been successful. Since I am running out of ideas (I tried few similar things as above) I am turning to the community for some help on this.

M.M

Your question is not very clear but I am assuming you want to build a string showing , in hex, the values of the first four bytes pointed to by index.

The code could be:

char outVal[9];
sprintf(outVal, "%02X%02X%02X%02X", index[0], index[1], index[2], index[3]);

Do not try this with char * pointer, that could lead to undefined behaviour. It relies on index being unsigned char *.

If you want to print many more bytes, then you can switch to a loop, for example:

std::vector<char> outVal( 2 * len + 1 );
for (size_t i = 0; i < len; ++i)
    sprintf(&outVal[i * 2], "%02X", index[i]);

// OutputDebugString(&outVal[0]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert an unsigned char array into a string in C?

How to convert an unsigned char variable that holds two hexadecimal values into two chars with one hex number

How do I cast a const char* to a const unsigned char*

How do I store _m128 SSE register data on an "unsigned char" array?

How to convert char array to hexadecimal

Save integer array in hexadecimal format to a char "string"

How do I change 2+ values in a char array?

Python - How do I format a hexadecimal number in uppercase and two digits?

How do I cast a slice of memoryview into C string (unsigned char*)?

Convert hexadecimal string to unsigned char[]

I want to convert a hexadecimal byte array to hexadecimal in string format

How to interpret an unsigned char array as hex array?

How do I change a char's values?

Format specifier for unsigned char

How to copy a std::string to unsigned char array?

How to copy streambuf to unsigned char array?

How can I transform a char array to a string representing its value in hexadecimal?

how do I free this char** array?

How do I add String to a char array?

How do I insert char* to array of structures?

How do I use tolower() with a char array?

How can I convert unsigned int to unsigned char

how can I display unsigned char with SetWindowText

How do I read a stringstream into a char *[40] / char ** array?

How do I tokenize a char array input into a char and a string?

How do these hexadecimal values return true?

How do I convert a hexadecimal number in IEEE 754 double precision (64bit) format to a decimal number?

How do I format a signed integer to a sign-aware hexadecimal representation?

How can I add a 64 bit floating point number to an unsigned char array at specific indexes (C++)