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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxz520zmg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值