ASCII转Hex算法
将接收到的ASCII字符串转为Hex并存入数组。
uint8_t _2ASCII_2_HEX(uint8_t *buff)
{
uint8_t i = 0 , temp = 0;
for(i=0;i<2;i++)
{
buff[i] -= 0x30;
if(buff[i] < 0x0A)
buff[i] = buff[i] & 0x0F;
else
buff[i] = (buff[i] + 10 - 65) & 0x0F;
}
temp = (buff[0] * 10) + buff[1];
return temp;
}
其中for循环中,i最大循环次数可修改,可根据想要转化ASCII的个数进行修改。
例如:3个ASCII转为Hex
uint8_t _3ASCII_2_HEX(uint8_t *buff)
{
uint8_t i = 0 , temp = 0;
for(i=0;i<3;i++)
{
buff[i] -= 0x30;
if(buff[i] < 0x0A)
buff[i] = buff[i] & 0x0F;
else
buff[i] = (buff[i] + 10 - 65) & 0x0F;
}
temp = (buff[0] * 100) + (buff[1] * 10) + buff[2];
return temp;
}
使用方法:
uint8_t AscIIBuff[2] = {0x31,0x32};
void Function(void)
{
uint8_t Hex_;
Hex_ = _2ASCII_2_HEX(AscIIBuff[0]);
}