This is driving me nuts because I don’t know the magic words to search for to find a solution.
I initialize:
unsigned char foo[] = {'0', 'x', '1', '1'};
unsigned char bar[] = {'\0'};
Essentially I want bar[0] to be equal to the value 0x11. How do I do that? I feel stupid and I’m pretty sure there’s a simple solution but I can’t figure it out. I tried
memcpy(&bar[0], &foo[0], 1);
But that just makes bar[0] have the value of 0x30, which is the ascii value of the ‘0’ character.
Any help/hints?
In case you want to convert text values to integer, that can be done via
sscanffunction:Note that:
stringactually has five characters, as there is a terminating zero at the end. If you ran C-string-expecting function on unterminated array {‘0’, ‘x’, ‘1’, ‘1’} you had above, if would continue processing data past the array, which is something you definitely do not want.