Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 13


 int ascii_hex_to_byte(char upper_nibble, char lower_nibble)
{
// Return the byte value represented by the ASCII Hex
// characters upper_nibble and lower_nibble
// Return -1 on failure.


byte return_value = 0;

// Convert each char into its value.

switch (upper_nibble)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
upper_nibble -= 48;
break;
}
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
{
upper_nibble -= 87;
break;
}
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
{
upper_nibble -= 55;
break;
}
default:
{
return_value = -1;
break;
}
}
Free download pdf