I am looking at a simple C function created by Tibor Kiss (link below). I am trying to understand how converting a single binary byte to two hex characters involves the addition of 'W' (0x57). Why is this being done?
I understand that >> shifts the character c right by four places (filling in the lefthand bits with 0's). I also understand the x=c&0x0f part which just masks the upper four bits of x using bitwise AND.
I just don't know why converting a binary byte to hexidecimal would involve adding ASCII 'W' (0x57).
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en543105
/******************************************************************************
* Function: void btoh(unsigned char c,char *str)
*
* PreCondition: None
*
* Input: str - pointer to the zero terminated string
* c - byte to convert
*
* Output: None
*
* Side Effects: None
*
* Overview: Convert one byte to a 2 character length hexadecimal
* zero terminated string
*
* Note: Using static variable for less code size
*****************************************************************************/
void btoh(unsigned char c,char *str)
{
static unsigned char x;
x=c>>4;
*str=x+(x>9?'W':'0');
x=c&0x0f;
str[1]=x+(x>9?'W':'0');
str[2]=0;
}