Usually I see that the homebrew implementation of the hex2dec functions is as follows:
unsigned char hex2dec_nibble(unsigned char n)
{
// Numbers
if(n >= 0x30 && n <= 0x39)
{
return (n-0x30);
}
// Upper case
else if(n >= 0x41 && n <= 0x46)
{
return (n-0x41+10);
}
// Lower case
else if(n >= 0x61 && n <= 0x66)
{
return (n-0x61+10);
}
else
{
return -1;
}
}
int main()
{
unsigned char t;
t = '0'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'A'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'F'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'G'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'a'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'f'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = 'g'; printf("%c = %d\n", t, hex2dec_nibble(t));
t = '='; printf("%c = %d\n", t, hex2dec_nibble(t));
}
What is displayed:
0 = 0
A = 10
F = 15
G = 255
a = 10
f = 15
g = 255
= = 255
, .
. #include printf, hex2dec_nibble. .