数据格式转换

字符串转十六进制字符数组:

 1         char cDataHigh,cDataLow;
 2         int iSendDataSize = 0;
 3         int iSize = Str.size();
 4         for(int i = 0;i<iSize-1;)
 5         {
 6             char tempByte_h =  Str.at(i).toLatin1();
 7             if(tempByte_h == ' ')//去掉字符串内首部的空格
 8             {
 9                 i+=1;
10                 continue;
11             }
12             else
13             {
14                 char tempByte_l = Str.at(i+1).toLatin1();//转化成ASCII码的形式,便于符号之间的运算
15                 i+=2;
16                 cDataHigh = MainWindow::convertHexFromChar(tempByte_h);//高位
17                 cDataLow  = MainWindow::convertHexFromChar(tempByte_l);//低位
18                 char sendChar =(char)((cDataHigh<<4)+cDataLow);//组合
19                 sendData[iSendDataSize]=sendChar;
20                 iSendDataSize++;
21             }

 static char convertHexFromChar(char ch)

        {
            if((ch>='0')&&(ch<='9'))
            {
                return(ch-0x30);
            }
            else if((ch>='A')&&(ch<='F'))
            {
                return(ch-'A'+10);
            }
            else if((ch>='a')&&(ch<='f'))
            {
                return(ch-'a'+10);
            }
            else
            {
                return(-1);
            }
        }

字节数组转16进制字符串:

1 static QString ByteArrayToHexStr(QByteArray data) {
2     QString temp = "";
3     QString hex = data.toHex();
4     for (int i = 0; i < hex.length(); i = i + 2) {
5         temp += hex.mid(i, 2) + " ";
6     }
7     return temp.trimmed().toUpper();
8 }

 

转载于:https://www.cnblogs.com/feaker/p/7592301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值