16,466
社区成员
发帖
与我相关
我的任务
分享
BOOL Translate(char* szIn, BYTE* szOut, int* nOutLen)
{
if(NULL == szIn || NULL == szOut)
{
return FALSE;
}
int len = strlen(szIn);
if(0 != len%2)
{
return FALSE;
}
char* buf = NULL;
buf = new char[len+1];
strcpy(buf, szIn);
int i=0;
int j = 0;
for(; i<len; i++)
{
if((buf[i] >= '0') && (buf[i] <= '9'))
{
buf[i] -= '0';
}
else if((buf[i] >= 'A') && (buf[i] <= 'F'))
{
buf[i] -= ('A' - 10);
}
else if((buf[i] >= 'a') && (buf[i] <= 'f'))
{
buf[i] -= ('a' - 10);
}
else
{
delete buf;
buf = NULL;
return FALSE;
}
}
i = 0;
while(i < len)
{
if(j >= *nOutLen)
{
break;
}
szOut[j++] = (buf[i] * 16 + buf[i+1]) & 0xFF;
i += 2;
}
*nOutLen = j;
delete buf;
buf = NULL;
return TRUE;
}
// 调用
char* szText = "0123456789ABCDEFFEDCBA9876543210";
BYTE buf[16] = {0};
int nLen = 16;
Translate(szText, buf, &nLen);
CString strText;
CString strTmp;
for(int i=0; i<nLen; i++)
{
strTmp.Format(_T("0x%02X\r\n"), buf[i]);
strText += strTmp;
}
AfxMessageBox(strText);
CString str="0123456789ABCDEFFEDCBA9876543210";
char buf[16];
for (int i=0 ;i<16;i++)
{
buf[i]=strtoul(str.Mid(i,2),0,16);
}