Can anyone suggert me how to convert a asccii character to a hexadecimal value (example: 38 asccii to 08 hex)

Thank

Wilmer

Can anyone suggert me how to convert a asccii character to a hexadecimal value (example: 38 asccii to 08 hex)

Thank

Wilmer

if you are using the result in a math function look up the Functions atof, atoi & atol.

If you are just going to be printing the result using printf then use %x

printf("%x

",38);

The functions atof, atoi & atol will not help you convert to hex. Also, these are string based functions that expect a null terminated string, not just a single character value. If you are actually looking to convert a string of hex digits into a value, then there is a string function that does this. The strtol function has a base parameter, set that to 16 and you get hex conversions of a string.

If what you want is to convert just a single character value, then this can be done by the following:

char ascii, hex_value;

ascii = ‘8’;

ascii = toupper(ascii);

if (isxdigit(ascii)) hex_value = ascii - (ascii > 0x39 ? 0x37 : 0x30);

This basic formula assumes that ascii contains a valid hex digit (in ascii, of course) and that all hex digits A thru F are given in upper case. If these conditions are guaranteed, then the toupper call and if (isxdigit(ascii)) are not necessary.

Seulater,

Thank by your help, I heve a string of asccii character and want to get out it by a port of 8 bits, I must to take 2 characters toguether to get out by the port (example: 31,39 ASCII , like 19 hex)

Wilmer

Opps! i meant to type strtol, sorry guys…:o