Qdebug display hex value

前端 未结 9 1500
你的背包
你的背包 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:45

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

    qDebug().noquote() << QString::number(value, 16);
    
    0 讨论(0)
  • 2021-02-19 03:48

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

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