ASCLL和hex转换

1.十六进制转换成十进制
int hex2num(char c)
{
if (c >= ‘0’ && c <= ‘9’)
return c - ‘0’;
if (c >= ‘a’ && c <= ‘f’)
return c - ‘a’ + 10;
if (c >= ‘A’ && c <= ‘F’)
return c - ‘A’ + 10;

return -1;

}
2.十进制转十六进制
char num2hex(uint8_t int)
{
if ((c + ‘0’) >= ‘0’ && (c + ‘0’) <= ‘9’)
return c + ‘0’;
if ((c - 10 + ‘a’) >= ‘a’ && (c - 10 + ‘a’) <= ‘f’)
return c - 10 + ‘a’;
if ((c - 10 + ‘A’) >= ‘A’ && (c - 10 + ‘A’) <= ‘F’)
return c - 10 + ‘A’;
return -1;
}

你可能感兴趣的:(ASCLL和hex转换)