C语言 16进制与ascii码互转

/*把ASCII字符转换为16进制 */
uint8_t char_to_hex(const uint8_t *ch)
{
uint8_t value = 0;
if(*ch >= 0 && *ch <= 9)
{
value = *ch + 0x30;
}
else if(*ch >=10 && *ch <=15)
{
// 大写字母
value = *ch + 0x37;
}
 
//printf("%s hex : 0x%02x\n", __func__, value);
return value;
}


/*把16进制转换为ASCII字符 */
uint8_t hex_to_char(const uint8_t ch)
{
uint8_t value = 0;
if(ch >= 0x30 && ch <=0x39)
{
value = ch - 0x30;
}
else if(ch >= 0x41 && ch <= 0x46)
{
value = ch - 0x37;
}
else if(ch >= 0x61 && ch <= 0x66)
{
value = ch - 0x57;
}

//printf("%s ascii : 0x%02x\n", __func__, value);
return value;
}

转载于:https://www.cnblogs.com/rohens-hbg/p/10123830.html

你可能感兴趣的:(C语言 16进制与ascii码互转)