Qdebug display hex value

前端 未结 9 1464
你的背包
你的背包 2021-02-19 02:56

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 quot

相关标签:
9条回答
  • 2021-02-19 03:23
    qDebug() << QByteArray::number(myNumber).toHex()
    
    0 讨论(0)
  • 2021-02-19 03:30

    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.

    0 讨论(0)
  • 2021-02-19 03:31

    You could format string first:

    int myValue = 0x1234;
    QString valueInHex= QString("%1").arg(myValue , 0, 16);
    qDebug() << "value = " << valueInHex;
    
    0 讨论(0)
  • 2021-02-19 03:32

    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;
    }
    
    0 讨论(0)
  • 2021-02-19 03:35

    The solution is simple:

    #include <QDebug>
    
    int value = 0x12345;
    qDebug() << "Value : " << hex << value;
    
    0 讨论(0)
  • 2021-02-19 03:39

    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.

    0 讨论(0)
提交回复
热议问题