18
votes

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

9
Did you try using the + Operator instead of << for concatenating the strings?Sebastian Lange
tried this but problem is half solved .... but yet full string is displayed with quotes .. ? How to remove this quotesKatoch
i get following output with quotes after replacing << with + .. "Heart-Beat : Msg ID = 720 Msg DLC = 1"Katoch
You remove quotes by using qDebug().noquote()David Casper

9 Answers

8
votes

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;
}
32
votes

The solution is simple:

#include <QDebug>

int value = 0x12345;
qDebug() << "Value : " << hex << value;
26
votes

You could format string first:

int myValue = 0x1234;
QString valueInHex= QString("%1").arg(myValue , 0, 16);
qDebug() << "value = " << valueInHex;
11
votes

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.

6
votes

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.

4
votes
qDebug() << QByteArray::number(myNumber).toHex()
2
votes

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));
2
votes

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

qDebug().noquote() << QString::number(value, 16);
1
votes
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