ascii to hex
I have a single char which I want to look up the hexadecimal value for. I couldn't find a plain ascii_to_hex function so my question is simple.. is there a function somewhere in the headerfiles or do I have to code my own?
i have a single 'A' and want to get the hex value for 'A' -> (41)
I managed to solve the problem and here is what I came up with...
If there is someone with a better solution just let me know.
unsigned long getHexValue(char ch)
unsigned long ret = 0x0;
/* mask the first 4 bits and shift 4 bits right */
ret = ((ch & 0xF0) >> 4);
/* shift 4 bits left */
ret = (ret << 4);
/* append the last 4 bits */
ret |= (ch & 0x0F);
as far as i understand (which isn't that far) hex numbers == decimal numbers
an ascii char is a value. you need to put it in a string or something. to print it in hex you could do this:
i'll bet that printf has a way of printing hex without this. i'll almost positive of it. and cout probably can, too.
unsigned char value = 56; // any value < 256 will do
let1 = value / 16;
let2 = value % 16;
if (let1 > 9) let+='A' - 10;
if (let2 > 9) let+='A' - 10;
cout << let1 << let2;
cout << hex << (int)'A' << endl;
cout << hex << endl;