how to convert from Hex String to Byte Array ? Hex String to Byte Array 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BYTE buf[100] buf=(BYTE*)(LPCTSTR)CStringVar 我说的CStringVar 是16进制的String,比如1A 08 3E 4F......我想不可以这样做吧? 那就先用这个函数我好象没有看到过,只能自己写了!我很早写过类似这个功能的代码先转化成 char* , 并得到长度 nLen;BYTE* btResult = new BYTE[nLen/2];BYTE n[3];for(j=0; j<nLen/2*2; j++){ if(strRec[j]==' ') { n[0]=0; j++; } else n[0]=strRec[j++]-48; n[1]=strRec[j]-48; n[2]=n[0]*16+n[1]; } btResult[j] = n[2];}放到你的程序里面去试试看! 空格的意思就是 当出现 1A , _B,就是只有一个B,前面应该有一个空格,如果你没有空格那就没有办法转换了! CByteArray byArray;BYTE* pByteBuf = new BYTE[cNum];// 在pByteBuf里填入1A 08 3E 4F......// ....// 下面做CByteArray的转换byArray.SetSize(cNum);memcpy(byArray.GetData(), pByteBuf, cNum * sizeof(BYTE)); n[0]=strRec[j++]-48; n[1]=strRec[j]-48; 感谢您的回答!!! 减48 是什么意思?您能解释一下吗? 48是什么, '0'的ASCII码值。字符串本身都是ASCII码值,要想得到真正数值必须减去48,但原程序不直观n[0]=strRec[j++]-'0'; n[1]=strRec[j]-'0';这样就好懂了吧! //假设十六进制串是两个代表一个字节CByteArray byArray;BYTE tem;for(j=0; j<nLen; j++){ // skip the space if (strRec[j]==' ') j++; if (j >= nLen) break; else tem = strRec[j ++] - '0'; if (j < nLen) tem = tem * 16 + (strRec[j]- '0'); byArray.Add(tem);}byArray.GetSize()是 Byte Array的长度byArray[i]用于访问具体的字节 原来的for(j=0; j<nLen/2*2; j++)方式会导致 btResult溢出BYTE* btResult = new BYTE[nLen/2];还是改成这样好:// CString strVal = "1A 08 3E 4F......";;long nLen = strVal.GetLength();//假设十六进制串是两个代表一个字节CByteArray byArray;BYTE tem;long j;for(j=0; j < nLen; j++){ // skip the space while (strVal[j]==' ' && j < nLen) j++; if (j >= nLen) break; else tem = strVal[j ++] - '0'; if (j < nLen && strVal[j] != ' ') tem = tem * 16 + (strVal[j]- '0'); byArray.Add(tem);}byArray.GetSize()是 Byte Array的长度byArray[i]用于访问具体的字节 虽然不太正确,我还是给分了问题出在char >9 时出错 怎样使tooltip弹出信息不被wndTopMost窗口遮盖? Lib引入和使用 问一个关于msdn使用的问题 求教 如何删除文件 怎么自己定义文件对话框,不是CFileDialog 请大家帮帮忙,一个ADO连接的问题 怎样获得listbox当前选中的项的index? 以下是什么错误啊? 如何通过API函数去获取指定系统的系统补丁 ★★为什么包含RichEdit的对话框不能DoModal()★★ 请问:哪儿能下载: Visual C++ 6.0编程高手(希望出版社)
buf=(BYTE*)(LPCTSTR)CStringVar
我想不可以这样做吧?
这个函数我好象没有看到过,只能自己写了!我很早写过类似这个功能的代码先转化成 char* , 并得到长度 nLen;BYTE* btResult = new BYTE[nLen/2];
BYTE n[3];
for(j=0; j<nLen/2*2; j++)
{
if(strRec[j]==' ')
{
n[0]=0;
j++;
}
else
n[0]=strRec[j++]-48;
n[1]=strRec[j]-48;
n[2]=n[0]*16+n[1];
}
btResult[j] = n[2];
}
放到你的程序里面去试试看!
BYTE* pByteBuf = new BYTE[cNum];// 在pByteBuf里填入1A 08 3E 4F......
// ....// 下面做CByteArray的转换byArray.SetSize(cNum);
memcpy(byArray.GetData(), pByteBuf, cNum * sizeof(BYTE));
n[1]=strRec[j]-48;
感谢您的回答!!! 减48 是什么意思?您能解释一下吗?
n[1]=strRec[j]-'0';这样就好懂了吧!
//假设十六进制串是两个代表一个字节
CByteArray byArray;
BYTE tem;
for(j=0; j<nLen; j++)
{
// skip the space
if (strRec[j]==' ')
j++; if (j >= nLen)
break;
else
tem = strRec[j ++] - '0';
if (j < nLen)
tem = tem * 16 + (strRec[j]- '0');
byArray.Add(tem);
}byArray.GetSize()是 Byte Array的长度
byArray[i]用于访问具体的字节
BYTE* btResult = new BYTE[nLen/2];还是改成这样好:// CString strVal = "1A 08 3E 4F......";;long nLen = strVal.GetLength();
//假设十六进制串是两个代表一个字节
CByteArray byArray;
BYTE tem;
long j;
for(j=0; j < nLen; j++)
{
// skip the space
while (strVal[j]==' ' && j < nLen)
j++; if (j >= nLen)
break;
else
tem = strVal[j ++] - '0';
if (j < nLen && strVal[j] != ' ')
tem = tem * 16 + (strVal[j]- '0');
byArray.Add(tem);
}byArray.GetSize()是 Byte Array的长度
byArray[i]用于访问具体的字节
问题出在char >9 时出错