//功能描述:把ASCII字符转换为16进制
uint8_t CharToHex(uint8_t bHex)
{
if((bHex>=0)&&(bHex<=9))
{
bHex += 0x30;
}
else if((bHex>=10)&&(bHex<=15))//Capital
{
bHex += 0x37;
}
else
{
bHex = 0xff;
}
return bHex;
}
//功能描述:把16进制转换为ASCII字符
uint8_t HexToChar(uint8_t bChar)
{
if((bChar>=0x30)&&(bChar<=0x39))
{
bChar -= 0x30;
}
else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
{
bChar -= 0x37;
}
else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
{
bChar -= 0x57;
}
else
{
bChar = 0xff;
}
return bChar;
}
如果发送tx_data = "6803A00AC1",而接收到的数据是rx_data = {0x36, 0x38, 0x30, 0x33, 0x41, 0x30, 0x30, 0x41, 0x43, 0x31},想要将接收的数据转换成{0x68, 0x03, 0xA0, 0x0A, 0xC1},可以使用以下方法:
/* 将传下来的Acsii码转换成Hex */
for(uint8_t i = 0; i < sizeof(rx_data ); i += 2)
{
hex_rx_buff[index++] = HexToChar(rx_data[i]) * 16 + HexToChar(rx_data[i+1]);
}
网友评论