c代码 ASCII转16进制
static u8 asciiToHex(u8 asciiCode)
{
if ((asciiCode >= '0') && (asciiCode <= '9'))
{
return asciiCode - '0';
}
else if ((asciiCode >= 'a') && (asciiCode <= 'f'))
{
return asciiCode - 'a' + 10;
}
else if ((asciiCode >= 'A') && (asciiCode <= 'F'))
{
return asciiCode - 'A' + 10;
}
else
{
return 0xff;
}
}
static int convertToHex(u8 *phex, u8 *pasciiCode, int len)
{
u8 hextemp;
int step = 2;
if (len % 2)
{
return 0;
}
for (int i = 0; i < len/step; i++)
{
hextemp = asciiToHex(pasciiCode[i*step]);
phex[i] = (hextemp << 4) | (asciiToHex(pasciiCode[i*step+1]));
}
return 1;
}