CString的16进制转换成BYTE数组

//封装

BYTE CSingleDlg::Hex2Char( BYTE by )

{
if((by>='0')&&(by<='9'))  
return by-0x30;     
else if((by>='A')&&(by<='F'))       
return by-'A'+10;      
else if((by>='a')&&(by<='f'))       
return by-'a'+10;             
else         return -1; 

}

//函数

BYTE CSingleDlg::CStringHex2Byte( BYTE * byBuffer, CString strInput )
{
int nLen = strInput.GetLength();
BYTE byTemp = 0x00;
for ( int i=0, j = 0; i < 1024, j < nLen; i++,j++)
{
if( strInput[j] == '\0')
return 0;
if( strInput[j] == ' ')
++j;
byTemp = strInput[j];
byBuffer[i] = Hex2Char( byTemp )<<4;//左移4位
if( strInput[j+1] == '\0')
return 0;
if(  strInput[++j] == ' ' )
++j;
byTemp = strInput[j];
byBuffer[i] = byBuffer[i] + (Hex2Char( byTemp ) & 0xF);//取低四位相加
}
return 0;
}


使用:

CString str = _T("30 30 32 32");
BYTE byBuff[1024];
memset( byBuff, '\0', 1024);
CStringHex2Byte(byBuff,str);

结果:

bybuff[] = {0022};

你可能感兴趣的:(CString16进制BYTE)