CString类型字符串,转换到 BYTE类型
2009-03-07 15:53
316 查看
VC中,MSComm通信控件,使用串口进行数据通信,
从Edit Box获取字符串CString,转换为BYTE,再转换为CByteArray,
然后转换为COleVariant,发送出去。
************************************************************************
方法一:
CString text="a0";
int d = 0;
sscanf(text, "%x", &d);
BYTE bt = (BYTE)d;
具体:
UpdateData(TRUE); // 从Edit Box获取字符串CString m_strSendASCII
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
= static_cast<BYTE>(atoi(strTemp2.GetBuffer(10)));
int int_temp = 0;
sscanf(strTemp, "%02x", &int_temp);
byte
= (BYTE)int_temp;
i += 2;
n++;
strTemp.ReleaseBuffer();
}
for (n = 0; n < str_length / 2; n++)
{
//arraySend.SetAt(n, byte
); // arraySend.SetSize(10); 如果发送少于10个字节,那会补0,到10个字节
arraySend.Add(byte
);
}
m_MSComm1.SetOutput(COleVariant(arraySend));
}
********************************************************************************************************
方法二:
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));
}
从Edit Box获取字符串CString,转换为BYTE,再转换为CByteArray,
然后转换为COleVariant,发送出去。
************************************************************************
方法一:
CString text="a0";
int d = 0;
sscanf(text, "%x", &d);
BYTE bt = (BYTE)d;
具体:
UpdateData(TRUE); // 从Edit Box获取字符串CString m_strSendASCII
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
= static_cast<BYTE>(atoi(strTemp2.GetBuffer(10)));
int int_temp = 0;
sscanf(strTemp, "%02x", &int_temp);
byte
= (BYTE)int_temp;
i += 2;
n++;
strTemp.ReleaseBuffer();
}
for (n = 0; n < str_length / 2; n++)
{
//arraySend.SetAt(n, byte
); // arraySend.SetSize(10); 如果发送少于10个字节,那会补0,到10个字节
arraySend.Add(byte
);
}
m_MSComm1.SetOutput(COleVariant(arraySend));
}
********************************************************************************************************
方法二:
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));
}
相关文章推荐
- CString类型字符串,转换到 BYTE类型
- CString与string类型互相转换、CString转化为LPCSTR、TCHAR转化为CString、CString型字符串连接
- CString类型转换,字符串操作和UNICODE、ANSI字符集
- char*数组(c类型字符串)和Vc++中的CString的转换,char*数组等问题
- vc中各种类型字符串的转换,CString, BSTR, LPCTSTR 概念
- 移植性问题のCString类型转换,字符串操作和UNICODE、ANSI字符集
- VC字符串类型CString,int,char*,string互相转换代码实例
- CString TCHAR BSTR BYTE WORD DWORD COLORREF VARIANT等数据类型相互转换
- 将字符串CString类型转换成时间类型
- Java 数据类型转换——byte[]/16进制/字符串互转
- 如何将CString 的一个字符串转换成一个string 类型的
- 如何从 CString 转换为其他字符串类型
- C# 读取 timestamp 时间戳 值为byte[] 类型时,需要转换成 16进制的字符串 和 数据库中的时间戳值进行比对
- CString 操作指南(CString与其他字符串类型转换)
- unicode CString string char 等类型字符串转换
- 数据类型转字符串及CString与其他数据类型的转换
- CString与LPCWSTR、LPSTR、char*、LPWSTR等类型的转换 (UNICODE版本字符串转换为Ansi版本)
- CString类型转换,字符串操作和UNICODE、ANSI字符集
- 完成一程序演示字符数组、C风格字符串、std::string和MFC中的Cstring类型之间的相互转换
- 《C++标准函数库》中说的有三个函数可以将字符串的内容转换为字符数组和C—string (CString、char*、String三种类型互换)