分享

CString的16进制转换成BYTE数组

 yng326309158 2016-09-01

//封装

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多