急问:CString转 BYTE类型的问题

ysysbaobei 2009-03-06 05:27:33
CString转 BYTE类型

比如:
CString str = "AAAA580020";
要求转换为:
BYTE data[5];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x58;
data[3] = 0x00;
data[4] = 0x20;

往高手指点下,实在是做不出来了
...全文
986 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangdi7922 2011-09-02
  • 打赏
  • 举报
回复
学习了,sscanf太强大了
jia_xiaoxin 2009-03-07
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <string.h>


void AlphaToNum(char * ListChar, unsigned char * ReturnByte, int n)
{
unsigned char High;
unsigned char low;
for(int i = 0; i < n; i+=2)
{
if(ListChar[i] >= '0' && ListChar[i] <= '9')
{
High = ListChar[i] - '0';
}
if(ListChar[i] >= 'a' && ListChar[i] <= 'f')
{
High = ListChar[i] - 'a' + 10;
}
if(ListChar[i] >= 'A' && ListChar[i] <= 'F')
{
High = ListChar[i] - 'A' + 10;
}

if(ListChar[i+1] >= '0' && ListChar[i+1] <= '9')
{
low = ListChar[i+1] - '0';
}
if(ListChar[i+1] >= 'a' && ListChar[i+1] <= 'f')
{
low = ListChar[i+1] - 'a' + 10;
}
if(ListChar[i+1] >= 'A' && ListChar[i+1] <= 'F')
{
low = ListChar[i+1] - 'A' + 10;
}

ReturnByte[i] = High * 16 + low;
}
}

int main(int argc, char* argv[])
{
char * str = "AAAA580020";
int Len = strlen(str);
unsigned char * data = new unsigned char[Len];
AlphaToNum(str, data, Len);

printf("%X", data[0]);
printf("\n");
return 0;
}
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 rickyzhang2008 的回复:]
引用楼主 ysysbaobei 的帖子:
CString转 BYTE类型

比如:
CString str = "AAAA580020";
要求转换为:
BYTE data[5];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x58;
data[3] = 0x00;
data[4] = 0x20;

往高手指点下,实在是做不出来了


for(int i = 0, j = 0; j < ((int)str.size())/2; j += 2, ++i)
{
data[i]= 0;
data[i]= str[j];
data[i] < <= 4;
data[i] |= str[j + …
[/Quote]


for(int i = 0, j = 0; j < ((int)str.size())/2; j += 2, ++i)
{
data[i]= 0;
data[i]= str[j] - '0';
data[i] < <= 4;
data[i] |= str[j + 1] - '0';
}//字符转字节

有些错误,改下
  • 打赏
  • 举报
回复
[Quote=引用楼主 ysysbaobei 的帖子:]
CString转 BYTE类型

比如:
CString str = "AAAA580020";
要求转换为:
BYTE data[5];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x58;
data[3] = 0x00;
data[4] = 0x20;

往高手指点下,实在是做不出来了
[/Quote]】

for(int i = 0, j = 0; j < ((int)str.size())/2; j += 2, ++i)
{
data[i]= 0;
data[i]= str[j];
data[i] <<= 4;
data[i] |= str[j + 1];
}//字符转字节

上述代码可以作为参考,对lz应该有所帮助
neu_sunlei 2009-03-07
  • 打赏
  • 举报
回复
顶顶
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
顶顶
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
能否具体一点?
新手,搞了一天,网上找了N多资料,还没解决这个问题
hhyttppd 2009-03-07
  • 打赏
  • 举报
回复
sscanf
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
顶顶,高手指点下
在网上找了很多资料,都不行,
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hairetz 的回复:]
http://topic.csdn.net/t/20061031/13/5122055.html
这里有过讨论
[/Quote]

CString转 BYTE类型

比如:
CString str = "AAAA580020";
要求转换为:
BYTE data[5];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x58;
data[3] = 0x00;
data[4] = 0x20;

转换完后,
CByteArray byteArray;
byteArray.RemoveAll();
byteArray.SetSize(5);

for (int i = 0; i < 5; i++)
{
byteArray[i] = data[i];
}

m_MSComm1.SetOutput(COleVariant(byteArray)); // m_MSComm1是通信控件,把Edit Box里输入的字符串,转换为BYTE类型,再转换为CByteArray byteArray数据,通过计算机串口发送出去
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
顶上去,大家帮帮忙
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
结贴了,谢谢大家
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
方法二:sscanf(strTemp, "%02x", &int_temp);
strTemp = str.Mid(i, 2);


UpdateData(TRUE);

CByteArray arraySend;
arraySend.RemoveAll();
//arraySend.SetSize(10); 10必须为准确字节数,不然其后补0,满10位

BYTE byte[100];
int n = 0;
CString str = m_strSendASCII;

int str_length = str.GetLength();
CString strTemp;

// 用于统计字符串长度
CString str_Box;
str_Box.Format("%d", str_length);
MessageBox(str_Box);

// 字符串转换为 BYTE 类型
// 方式一:sscanf(strTemp, "%02x", &int_temp);
for (int i = 0; i < str_length; )
{
strTemp = str.Mid(i, 2);
//byte[n] = static_cast<BYTE>(atoi(strTemp2.GetBuffer(10)));
int int_temp = 0;

sscanf(strTemp, "%02x", &int_temp);
byte[n] = (BYTE)int_temp;

i += 2;
n++;
strTemp.ReleaseBuffer();
}

for (n = 0; n < str_length / 2; n++)
{
//arraySend.SetAt(n, byte[n]); // arraySend.SetSize(10); 如果发送少于10个字节,那会补0,到10个字节
arraySend.Add(byte[n]);
}
m_MSComm1.SetOutput(COleVariant(arraySend));
}
ysysbaobei 2009-03-07
  • 打赏
  • 举报
回复
谢谢楼上的,问题基本解决,找到了2中解决方法

方法一:

UpdateData(TRUE); // 从Edit Box获取字符串CString m_strSendASCII

CByteArray arraySend;
CString str = m_strSendASCII;
BYTE data[100];
BYTE temp;

if (str.GetLength() % 2 != 0)
{
MessageBox("输入字符串的字符个数不为偶数,请重新输入");
m_strSendASCII = "";
UpdateData(FALSE);
}
else
{
for(int i = 0; i < str.GetLength(); i += 2)
{
if ((str[i] >= '0') && (str[i] <= '9'))
{
data[i]= str[i] - '0';
}
else if ((str[i] >= 'a') && (str[i] <= 'z'))
{
data[i]= str[i] - 'a' + 10;
}
else if ((str[i] >= 'A') && (str[i] <= 'Z'))
{
data[i]= str[i] - 'A' + 10;
}
else
{
MessageBox("输入错误,请检查,并重新输入");
}
data[i] = data[i] << 4;


if ((str[i + 1] >= '0') && (str[i + 1] <= '9'))
{
temp= str[i + 1] - '0';
}
else if ((str[i + 1] >= 'a') && (str[i + 1] <= 'z'))
{
temp= str[i + 1] - 'a' + 10;
}
else if ((str[i + 1] >= 'A') && (str[i + 1] <= 'Z'))
{
temp= str[i + 1] - 'A' + 10;
}
else
{
MessageBox("输入错误,请检查,并重新输入");
}

data[i] |= temp;
arraySend.Add(data[i]);
}
m_MSComm1.SetOutput(COleVariant(arraySend));
}
}

  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20061031/13/5122055.html
这里有过讨论

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧