16进制数组和ASK码字符之间的转换

BIN 16进制数组
HEX ASK码字符串
#define PCD_NO_ERROR 0 // 操作成功执行
int HexToBin(unsigned char *shex,unsigned char *sbin,int shex_len)
{
int i;
const char *p = NULL;
unsigned char tmpHex[3];
int rc = shex_len;
int length = (int)strlen((char*)shex);
shex_len = length;
if (shex_len<=0 )
{
return -1;
}
if (length < shex_len* 2)
{
shex_len = length/2;
rc = shex_len;
}

// 每两个字符转换成一个BYTE
memset(tmpHex, 0x00, sizeof(tmpHex));
for (i = 0; i < shex_len; i++)
{
memcpy(tmpHex, shex + i * 2, 2);
if (tmpHex[0] >= '0' && tmpHex[0] <= '9')
{
tmpHex[0] = tmpHex[0] - '0';
}
else if (tmpHex[0] >= 'A' && tmpHex[0] <= 'F')
{
tmpHex[0] = tmpHex[0] - 'A' + 10;
}
else if (tmpHex[0] >= 'a' && tmpHex[0] <= 'f')
{
tmpHex[0] = tmpHex[0] - 'a' + 10;
}
else if (tmpHex[0] == ' ')
{
}
else
{
rc = i;
break;
}
if (tmpHex[1] >= '0' && tmpHex[1] <= '9')
{
tmpHex[1] = tmpHex[1] - '0';
}
else if (tmpHex[1] >= 'A' && tmpHex[1] <= 'F')
{
tmpHex[1] = tmpHex[1] - 'A' + 10;
}
else if (tmpHex[1] >= 'a' && tmpHex[1] <= 'f')
{
tmpHex[1] = tmpHex[1] - 'a' + 10;
}
else if (tmpHex[1] == ' ')
{
}
else
{
rc = i;
break;
}
sbin[i] = tmpHex[0] * 16 + tmpHex[1];
}

return rc;
}

猜你喜欢

转载自blog.csdn.net/m0_37759974/article/details/80405884