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

              }

 

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

 

方法二:

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

                     }                    

 

 

 

 

 

你可能感兴趣的:(byte,BT)