打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CString类型字符串,转换到 BYTE类型

CString类型字符串,转换到 BYTE类型



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[n] =   static_cast(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));                 

              }

 

********************************************************************************************************

 

方法二:

                     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));

                     }     

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
有关数据类型转换
splitString CString 类型的字符串分割
javascript常用验证函数
[JavaScript]几种常用的表单输入判断
【转】char*与LPCTSTR,LPTSTR的转换
CString, int, string, char*之间的转换_C语言教程_C++教程_...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服