Search code examples
qtqstringqbytearray

how to convert QBytearray to QString?


now I have a QByteArray object, I need to convert it to QString and print the content in HEX format. But I don't find correct method.

`QByteArray serial; 
serial.resize(4);
serial[0] = 0xA0;
serial[1] = 0x01;
serial[2] = 0x05;
serial[3] = 0xA6;`

QString str = "how to convert serial to QString"

convert "serial" object to QString object and the print format is "0xA0 0x01 0x05 0xA6"


Solution

  • Hope this one-liner solution will be useful:

    auto str = QString("0x%1").arg(QString(serial.toHex(':').toUpper())).replace(":"," 0x");