patricxian
patricxian

Reputation: 3

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"

Upvotes: 0

Views: 185

Answers (1)

simon_lee
simon_lee

Reputation: 16

Hope this one-liner solution will be useful:

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

Upvotes: 0

Related Questions