1. 程式人生 > >C++中char類型的十六進制字符串轉換成字節流

C++中char類型的十六進制字符串轉換成字節流

toupper 14. 轉換成 urn 類型 else con src for

如a[5]="1234"轉換成a[5]={0x12,0x34}

技術分享圖片

技術分享圖片

代碼如下:

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

C++中char類型的十六進制字符串轉換成字節流