# Thread: ascii to hex

1. ## ascii to hex

Hi everybody,

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?

example:

i have a single 'A' and want to get the hex value for 'A' -> (41)

Regards
-David-

2. ## solved.

I managed to solve the problem and here is what I came up with...

Code:
```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);

return ret;
}```
If there is someone with a better solution just let me know.

Regards
-David-

3. 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:
Code:
```unsigned char value = 56; // any value < 256 will do
char let1,let2;
let1 = value / 16;
let2 = value % 16;
if (let1 > 9) let+='A' - 10;
else let+='0';
if (let2 > 9) let+='A' - 10;
else let+='0';
cout << let1 << let2;```
i'll bet that printf has a way of printing hex without this. i'll almost positive of it. and cout probably can, too.