如a[5]="1234"转换成a[5]={0x12,0x34}

C++中char类型的十六进制字符串转换成字节流-LMLPHP

C++中char类型的十六进制字符串转换成字节流-LMLPHP

代码如下:

void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;

for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);

if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;

if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;

dest[i / 2] = (highByte << 4) | lowByte;
}
return ;
}

int main(void)
{
BYTE bufASC[1024]={0};
char ha[7] = "ef2233";
unsigned char temp[200]={0};
HexStrToByte(ha,temp,6);

05-11 13:42