QByteArray::fromHex() 、QByteArray::toHex()原理及使用

[static] QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)

Returns a decoded copy of the hex encoded array hexEncoded. Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

QByteArray text;
text = QByteArray::fromHex("51 74 20 69 73 20 67 72 65 61 74 21"); //表示从十六进制数转成字符串。
qDebug()<<"QByteArray::fromHex:"<<text; //QByteArray::fromHex: "Qt is great!"

QByteArray ascii;
ascii = text.toHex(' '); //表示从字符串转成十六进制数。该十六进制编码使用数字0-9和字母a-f。
qDebug()<<"text.toHex:"<<ascii; //text.toHex: "51 74 20 69 73 20 67 72 65 61 74 21"


 

QByteArray::fromHex,表示从十六进制数转成字符串。对其转换原理做简要说明。举例

    

    QString hstring1("31323235");
    QByteArray test1 = hstring1.toLocal8Bit();
    QByteArray test2 = QByteArray::fromHex(test1);

运行结果

变量test1用8个字符保存“31323235”,每个字符8位,两个字符16位。QByteArray::fromHex转换时,每16位作为转换单元。转换步骤:

第一步,把“31323235”分成4组,“31”,“32”,“32”,“35”

第二步,以“31”为例,fromHex表示从16进制。所以把31当做16进制数,转成十进制数是3*16+1=49. 结合ASCII码对照表,十进制数49对应数字1,把数字1当做字符“1”。其他3组类似。

                            ASCII码对照表,数字(0-9)

所以最终结果:

“31”->“1”

“32”->“2”

“32”->“2”

“35”->“5”

即变量test2的值是“1225”

QByteArray QByteArray::toHex() const

Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.
See also fromHex().

QByteArray QByteArray::toHex(char separator) const

This is an overloaded function.
Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.
If separator is not ‘\0’, the separator character is inserted between the hex bytes.

 QByteArray macAddress = QByteArray::fromHex("123456abcdef");
  macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
  macAddress.toHex(0);   // returns "123456abcdef"

This function was introduced in Qt 5.9.

返回QByteArray类型的十六进制编码副本。该十六进制编码使用数字0-9和字母a-f。
如果分隔符不是’\0’或0,分隔符将插入十六进制字节之间。

QByteArray byteArray = QByteArray::fromHex("1a2b3c4d5e6f");
byteArray.toHex(':');    // returns "1a:2b:3c:4d:5e:6f"
byteArray.toHex(0);      // returns "1a2b3c4d5e6f"
byteArray.toHex('\0');   // returns "1a2b3c4d5e6f"

有时候从网络获取到一串的mac地址却是没有使用":"或空格分隔开来,未免会让用户阅读困难起来。在Qt5.9及其以上版本QByteArray的toHex接口或许可以解决你的问题。

实例:



QByteArray Serial::sendHexCommand(QString cmd, int recvlen)
{

    QByteArray data  = cmd.toLocal8Bit();  //toUtf8()

    data  = QByteArray::fromHex(data); //    表示从十六进制数转成字符串。两位变一位

    uint16_t wcrc = this->ModbusCRC16(data);
    qDebug()<<"CRC low8:"<<QString("%1").arg(uint8_t(wcrc), 2, 16,QLatin1Char('0')) //不足4位补0
            <<"CRC high8:"<<QString("%1").arg(uint8_t(wcrc>>8), 2, 16,QLatin1Char('0'));

    //低位在前,高位在后
    data.append(char(wcrc));
    data.append(char(wcrc>>8));

    qDebug()<<"cmdSend:"<<data.toHex(' ');

//    m_serialPort->setReadBufferSize(recvlen);

    m_serialPort->write(data);
    m_serialPort->waitForBytesWritten(1000);

//    QThread::msleep(100);

    QByteArray buff;

    qDebug()<<"waitForReadyRead before .......";

    while(m_serialPort->waitForReadyRead(5000))
    {

        qDebug()<<"waitForReadyRead after1 .......";
        QElapsedTimer t;
        t.start();
        while(t.elapsed()<100) QCoreApplication::processEvents();

        qDebug()<<"waitForReadyRead after2 .......";

//        buff = m_serialPort->read(recvlen);
        buff = m_serialPort->readAll();
        qDebug()<<"buff:"<<buff.toHex(' ');

        if(buff.isEmpty())
        {
            qDebug()<<"buff is null !!!!!!!!";
        }

        break;
    }

    qDebug()<<"waitForReadyRead after3 .......";

    if(buff.isEmpty())
    {
        qDebug()<<"buff is null  timeout !!!!!!!!";
    }

    return buff;

}


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高亚奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值