Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert from character to hex

Status
Not open for further replies.

JGSH

Programmer
Feb 23, 2003
6
US
Hi all,

How can I convert a character to a hexidecimal, then parse each.

For example, after it is converted to say... 1E, how can I put 1 into a variable and E into another?

Thank you
 
Not sure I completely understand what you need but is this what you mean

char c = 0x1E;

char hexBuffer[12];
sprintf(hexBuffer,"%x",(int)c);

now hexBuffer contains the string 1E. You can get each value out of there by looking at specific indices. To convert the value back you can use strtol with a base of 16

Matt
 
You could also extract the half-bytes (nybbles) by the following method

right_nybble = (mybyte & 0x0F);
left_nybble = (mybyte & 0xF0) >> 4;

Cheers - Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top