例如:
CString sztmp = "40 40 50 65 ";
BYTE char[3]; //如何把如上的sztmp 每两位为一个十六进制数放到 下面的char数据中
char[0] = 0x40;
char[1] = 0x40;
char[2] = 0x50;
char[3] = 0x65;
方法:
一:char[4]里面是16进制的数, 放到CString,char[100]数组中比较容易
1 sscanf(sztmp, "%c %c %c %c ", &char[0], &char[1], &char[2], &char[3]);
2 char lpstr[100];
sprintf(lpstr,"%02X%02X%02X%02X", char[0],char[1],char[2],char[3]);
二: 下面代码我调试过 ok
CString CProgramInfoToolDlg::xByte2String(BYTE* bytes,int nArrSize)
{
static char hexDigits[] = { '0 ', '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', 'A ', 'B ', 'C ', 'D ', 'E ', 'F '};
char *chars = new char[nArrSize * 2+1];
for (int i = 0; i < nArrSize; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b > > 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
chars[nArrSize * 2] = '/0 ';
CString str = chars;
delete chars;
return str;
}
void xString2Byte(BYTE* bytes,int nByteSize,CString str)
{
BYTE tmp;
BYTE ntmp;
int bcount=0;
if(str.GetLength()==1)
{
str = "0 " + str;
}
for(int i =0; i <nByteSize*2 ; i++)
{
int a = str[i];
if(a> =48&&a <=57)
{
a= a-48;
}
else if(a> =65&&a <=70)
{
a= a-55;
}
else if(a> =97&&a <=122)
{
a= a-87;
}
else
{
MessageBox( "Type Error ");
ASSERT(0);
return;
}
tmp = a;
if(i%2 ==0)
ntmp = tmp < <4;
else
{
ntmp = (ntmp | tmp);
bytes[bcount] = ntmp;
bcount ++;
}
}
}