I use the following function to convert a value in hex to ASCII:
void hex_to_ascii(unsigned char* buf, unsigned char number)
{
int i;
sprintf( buf, "%03d", number );
printf("\nNumber in Ascii: ");
for( i=0; i< 3; i++)
printf("%#04x ", *(buf+i));
printf("\n");
return;
}
e.g. If the hex value is 0xff
the function generates 0x32 0x35 0x35
which is the Ascii representation of 255
.
Now I would like to reverse the operation. I would like a function that can be passed three bytes (array of 3) to generate the hex value.
i.e. We pass 0x32 0x35 0x34
and it generates 0xFE.
Can you please help me with this? It is for an embedded system, the values are not typed they are obtained from a comms buffer.
Many Thanks.
EDIT:
I wrote this and it seems to work..
unsigned char ascii_to_hex(unsigned char* buf)
{
unsigned char hundred, ten, unit, value;
hundred = (*buf-0x30)*100;
ten = (*(buf + 1)-0x30)*10;
unit = *(buf+2)-0x30;
value = (hundred + ten + unit);
printf("\nValue: %#04x \n", value);
return value;
}
Thanks to everyone for replying.