How to output all QByteArray data in hex with qDebug()?
Solved
General and Desktop
-
Hello,
I'm trying to debug some code using QByteArray. When I try to output the data with qDebug() I get something like this:
code: QByteArray ba.resize(5); ba[0] = 0x02; ba[1] = 0x00; ba[2] = 0x07; ba[3] = 0x61; ba[4] = 0x01; qDebug() << ba; output: "\x02\x00\x07"a"\x01"
Is there anyway to make the qDebug() output show the ASCII characters in hex also? I was expecting the output to look like:
output: "\x02\x00\x07\x61\x01"
Thanks
-
@bigguiness said in How to output all QByteArray data in hex with qDebug()?:
Is there anyway to make the qDebug()
I can find something at https://doc.qt.io/qt-6/qtextstream.html#qtextstream-manipulators so you have to convert it by yourself.
-
Yes as @Christian-Ehrlicher says you'll have to add some code sprinkles yourself, say you do the qDebug() line like this:
qDebug().noquote() << "\\x" + ba.toHex(' ').replace(" ","\\x");
-
@hskoglund Thanks!
-
std::ostirngstream out; out << hex << 0x01U << 0x02U << 0x03U; qDebug << out.str().c_str();
use ostringtream since it will take stream conversion manipulators, and then output the str() value to qDebug