CString sztmp = "40 40 50 65";
BYTE char[3];
char[0] = 0x40;
char[1] = 0x40;
char[2] = 0x50;
char[3] = 0x65;
将这个字符串中的十六进制数据已ACSII码的形式写到BYTE数组,请高手指点.
5 个解决方案
#1
sscanf(sztmp, "%c %c %c %c", &char[0], &char[1], &char[2], &char[3]);
#2
这个很简单啊。按照空格,一个一个读取,读出来之后,比如:40
可以理解成: 4* 16 + 0 * 1 = 64 = 0x40
4*16 + 5* 1 = 69 = 0x45
可以理解成: 4* 16 + 0 * 1 = 64 = 0x40
4*16 + 5* 1 = 69 = 0x45
#3
不对三楼说,那字符串要是CString sztmp = "a1 ff dc a0";
这样的就不行了!,还有其他办法吗?
这样的就不行了!,还有其他办法吗?
#4
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;
}
{
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;
}
#5
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 ++;
}
}
}
{
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 ++;
}
}
}
#1
sscanf(sztmp, "%c %c %c %c", &char[0], &char[1], &char[2], &char[3]);
#2
这个很简单啊。按照空格,一个一个读取,读出来之后,比如:40
可以理解成: 4* 16 + 0 * 1 = 64 = 0x40
4*16 + 5* 1 = 69 = 0x45
可以理解成: 4* 16 + 0 * 1 = 64 = 0x40
4*16 + 5* 1 = 69 = 0x45
#3
不对三楼说,那字符串要是CString sztmp = "a1 ff dc a0";
这样的就不行了!,还有其他办法吗?
这样的就不行了!,还有其他办法吗?
#4
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;
}
{
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;
}
#5
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 ++;
}
}
}
{
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 ++;
}
}
}