/***************************************************************************************************
* Function: 将字符串转换成十六进制数据
* input parameter:
* u8 *pbSrc: 输入的字符串
* u32 len: 输入数据长度
* output parameter:
* u8* pbDest: 输出的十六进制数据
* return:
* 0: 转换失败
* 非0: 转换成功
****************************************************************************************************/
u32 StrToHex(u8 *pbDest, u8 *pbSrc, u32 len)
{
char h1, h2;
unsigned char s1, s2;
int i;
for (i=0; i<len; i++)
{
h1 = pbSrc[2*i];
h2 = pbSrc[2*i+1];
if((h1>=0x30) && (h1<=0x39)) //0~9
s1 = h1 - 0x30;
else if((h1>=65) && (h1<=70)) //A~F
s1 = h1 - 55;
else if((h1>=97) && (h1<=102)) //a~f
s1 = h1 - 87;
else
return 0;
if((h2>=0x30) && (h2<=0x39)) //0~9
s2 = h2 - 0x30;
else if((h2>=65) && (h2<=70)) //A~F
s2 = h2 - 55;
else if((h2>=97) && (h2<=102)) //a~f
s2 = h2 - 87;
else
return 0;
pbDest[i] = s1*16 + s2;
}
return i;
}
字符串转成16进制数
于 2025-03-17 11:30:38 首次发布