I have a cstring object str = "5043", now i want to convert to Hex and put it in UCHAR array like
UCHAR sample[2];
Sample[0] = 0X50
Sample[1] = 0X43
How can i do this? please advice me
I have a cstring object str = "5043", now i want to convert to Hex and put it in UCHAR array like
UCHAR sample[2];
Sample[0] = 0X50
Sample[1] = 0X43
How can i do this? please advice me
You can scan the hex values directly from the string using sscanf(), something like below:
UCHAR sample[2];
for ( int i = 0; i < str.length() / 2 ; i++) {
sscanf( (str.substr(i*2,2)).c_str(), "%hx", &sample[i]);
}
h is for short and x is for hexadecimal obviously.
Also, this assumes that the UCHAR array is declared to be half as large as the string size.
Have you tried strol? It seems a little low tech, but should do the trick. Don't forget to pass 16 as the base...
You'll need to combine it with a little bit shifting and bitwise anding to split the result into exactly what you require, but that should be straightforward.
Hope this helps,