Qdebug display hex value
Asked Answered
S

11

24

I am trying to display a number using QDebug in the Hex format. Below is the code which I have written. It is working but the output has string contents enclosed in double quotes:

How to remove these quotes?

m_CanMsg_ptr->id = 0x720;
m_CanMsg_ptr->sizeData = 1;
m_CanMsg_ptr->data[0] = 0x05;

MessageID.setNum(m_CanMsg_ptr->id,16);
DataSize  = QString("%1").arg(m_CanMsg_ptr->sizeData, 0, 16);
data      = QString("%1").arg(m_CanMsg_ptr->data[0], 0, 16)

qDebug() << "Heart-Beat : Msg ID = " << MessageID << "  Msg DLC = " << DataSize;
qDebug() << "Msg Data " << " = " << data;

I did as per these resources:

http://forum.qt.io/topic/5003/moved-how-to-display-a-local-variable-in-hex-when-debugging-a-qt-app/2 http://qt.shoutwiki.com/wiki/Convert_hexadecimal_to_decimal_and_vice-versa_in_Qt

enter image description here

Stowaway answered 14/10, 2013 at 11:17 Comment(4)
Did you try using the + Operator instead of << for concatenating the strings?Nocturnal
tried this but problem is half solved .... but yet full string is displayed with quotes .. ? How to remove this quotesStowaway
i get following output with quotes after replacing << with + .. "Heart-Beat : Msg ID = 720 Msg DLC = 1"Stowaway
You remove quotes by using qDebug().noquote()Shaquitashara
O
8

qDebug is a debug interface. It's not meant for custom-formatted output, it's simply a way of quickly getting data in readable form. It's meant for a developer, and the quotes are there to remind you that you've output a string. qDebug() presumes that the const char* data is a message and shows it without quotes, other string types like QString are "just data" and are shown with quotes.

If you want custom formatting, don't use qDebug(), use QTextStream:

#include <QTextStream>
#include <cstdio>

QTextStream out(stdout);

void f() {
   out << "Heart-Beat : Msg ID = " << MessageID << "  Msg DLC = " << DataSize << endl;
}
Omalley answered 14/10, 2013 at 11:23 Comment(6)
Completeness it might be good to add: out << std::hex << MessageID << ...Escalator
out is not an std::ostream, so std::hex won’t compile. But hex all by itself will (it’s Qt’s, not std’s).Needless
The correct(?) way in Qt is Qt::hex(out) << number; or even Qt::showBase(Qt::hex(out)) << number; Or, for better readability IMHO: Qt::showbase(Qt::uppercasedigits(Qt::hex(out))) << number;Cilia
Sadly, to get what want, I needed: out << "0x" << QString("%1").arg(number, 8, 16, QLatin1Char('0')).toUpper(); Which just looks ugly. But it seems the simplest that gives me the same as printf("0x%08X", number);Cilia
@JesseChisholm Unfortunately, "streaming" string formatting was a failed STL experiment. Modern string fromatting eschews that old interface, and comes back to printf-like API in modern C++. But that's not in Qt yet.Needless
@UnslanderMonica I suppose this would be always a point of debate (also, new interface is nothing like printf). The problem of format string output is its fragility to usage, portability OR high cost, choose two of those. Problem of stl-like implementation was its annoying "I toggle it once and untoggle after" approach to output modes, but supposedly it had to generate a compile time , static sequence of operations upon stream, no excess memory allocation, stack crawling and parsing a string, no dependency on the actual types of arguments changing from platform to platform.Kirstinkirstyn
G
40

The solution is simple:

#include <QDebug>

int value = 0x12345;
qDebug() << "Value : " << hex << value;
Gesso answered 22/11, 2017 at 13:39 Comment(5)
"Furthermore, QTextStream manipulators can be piped into a QDebug stream." -- doc.qt.io/qt-5/qdebug.html#formatting-optionsBifilar
QTextStream also defines several global manipulator functionsBifilar
It should be noted that if the value is a char, this will not work, but can be easily resolved by casting the char to an int which then makes it work.Escalator
As of Qt 5.14, hex is exposed through the Qt namespace, Qt::hex, and you can use it with and without the Qt:: prefix. As of Qt 5.15, hex is deprecated and Qt::hex is the preferred usage.Ka
To show the 0x prefix, use Qt::hex << Qt::showbase << your_valueTundra
D
28

You could format string first:

int myValue = 0x1234;
QString valueInHex= QString("%1").arg(myValue , 0, 16);
qDebug() << "value = " << valueInHex;
Divulgence answered 13/8, 2014 at 9:19 Comment(0)
M
14

Another way of doing this would be:

int value = 0xFFFF;
qDebug() << QString::number(value, 16);

Hope this helps.

Edit: To remove the quotes you can cast the number as a pointer, as qt will format that without using quotes. For instance:

int value = 0xFFFF;
qDebug() << (void *) value;  

Slightly hackish, but it works.

Mundell answered 28/1, 2016 at 17:38 Comment(3)
+1 for being more efficient than qDebug() << QByteArray(...) (that produces a QString internally), but this too does not answer OP's question on how to get rid of the quotes.Carnarvon
I must have missed that, I updated my answer with something that will remove quotes.Mundell
Use noquote(), as in: qDebug().noquote() << QString::number(value, 16);Ka
O
8

qDebug is a debug interface. It's not meant for custom-formatted output, it's simply a way of quickly getting data in readable form. It's meant for a developer, and the quotes are there to remind you that you've output a string. qDebug() presumes that the const char* data is a message and shows it without quotes, other string types like QString are "just data" and are shown with quotes.

If you want custom formatting, don't use qDebug(), use QTextStream:

#include <QTextStream>
#include <cstdio>

QTextStream out(stdout);

void f() {
   out << "Heart-Beat : Msg ID = " << MessageID << "  Msg DLC = " << DataSize << endl;
}
Omalley answered 14/10, 2013 at 11:23 Comment(6)
Completeness it might be good to add: out << std::hex << MessageID << ...Escalator
out is not an std::ostream, so std::hex won’t compile. But hex all by itself will (it’s Qt’s, not std’s).Needless
The correct(?) way in Qt is Qt::hex(out) << number; or even Qt::showBase(Qt::hex(out)) << number; Or, for better readability IMHO: Qt::showbase(Qt::uppercasedigits(Qt::hex(out))) << number;Cilia
Sadly, to get what want, I needed: out << "0x" << QString("%1").arg(number, 8, 16, QLatin1Char('0')).toUpper(); Which just looks ugly. But it seems the simplest that gives me the same as printf("0x%08X", number);Cilia
@JesseChisholm Unfortunately, "streaming" string formatting was a failed STL experiment. Modern string fromatting eschews that old interface, and comes back to printf-like API in modern C++. But that's not in Qt yet.Needless
@UnslanderMonica I suppose this would be always a point of debate (also, new interface is nothing like printf). The problem of format string output is its fragility to usage, portability OR high cost, choose two of those. Problem of stl-like implementation was its annoying "I toggle it once and untoggle after" approach to output modes, but supposedly it had to generate a compile time , static sequence of operations upon stream, no excess memory allocation, stack crawling and parsing a string, no dependency on the actual types of arguments changing from platform to platform.Kirstinkirstyn
V
7

If one is not tied to use streaming operators, can go with the plain old %x and use qDebug with formatting string:

int hexnum = 0x56;
qDebug("My hex number is: %x", hexnum);

which will yield "My hex number is: 56", without quotes.

Vinegary answered 6/7, 2016 at 0:0 Comment(1)
FYI, this implies that hexnum is unsignedBreeder
A
4
qDebug() << QByteArray::number(myNumber).toHex()
Aronarondel answered 17/10, 2015 at 20:58 Comment(1)
This does not remove the quotes as asked. Also, this produces more internal overhead than necessary: QByteArray::number(myNumber) creates a QByteArray with the decimal representation which then needs a second step for converting to hex. Suggested improvement: qDebug() << QByteArray::number(myNumber,16).Carnarvon
K
4

Use noquote() to avoid the quotation marks. As in:

qDebug().noquote() << QString::number(value, 16);
Ka answered 6/9, 2016 at 21:0 Comment(0)
S
2

May be a little late, but in case someone needs this:

As statet in the answere by Kuber Ober, Qt only removes the quotes if it's a const char * passed to qDebug. However, Qt provides a macro to do the same with a QString - The qPrintable macro:

qDebug() << qPrintable(QString("String without quotes")) << QString("String with quotes");

This way, you can use the QString::number function (as provided by TheDancinZerg) to format the string:

int value = 0xFFFF;
qDebug() << qPrintable(QString::number(value, 16));
Smacking answered 8/2, 2016 at 9:45 Comment(0)
R
1
int value = 0xff005542;
qDebug() << QString("%1").arg(value , 0, 16).toUpper();

>>> FF005542

That'll give you hex output at whatever length is required. But if you'd like fixed width output, this is handy (example of four hex digits):

int value = 0xff;
qDebug() << QString("%1").arg(value, 4, 16, (QChar)'0').toUpper();
//      Here's the fixed field length⬆︎

>>> 00FF

Revulsion answered 23/6, 2018 at 14:19 Comment(0)
I
1

Just a note! The method using <<hex is deprecieted now, so it's recommended to use:


#include <QDebug>

int num = 0x4321;
qDebug() << "Hex num: " <<Qt::hex << num;

Because, if you compile this way just using <<hex you will got some warnings, what means that soon this form to write will not be compatible anymore. For more infos take a look at Qt documentation

Intendment answered 11/2, 2022 at 19:37 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Thersathersites
P
1

This is why you want to learn C before C++ & Qt:

qDebug("Data for test case 1: 0x%04X", crc)

Done, always works. No extra spaces, no extra quotes, no extra .noquote or << or ::hex or .toUpper etc.

Polysaccharide answered 5/3, 2023 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.