qDebug - 如何以位(二进制格式)输出数据

时间:2018-03-02 17:23:06

标签: c++ qt qt5 qdebug

qDebug()能否以二进制格式输出数据?
例如,我想检查一些状态变化:

unsigned char status;
...
qDebug() << "Status: " << status;

我想以二进制格式生成输出,例如:

Status: 1011

1 个答案:

答案 0 :(得分:11)

如果您想以二进制打印,可以使用:

  1. bin
  2. unsigned char status = 11;
    qDebug() << "Status:" << bin << status;
    
    Output:
    "Status: 1011"
    
    1. QString::number()
    2. unsigned char status = 11;
      qDebug() << "Status:" << QString::number(status, 2);
      
      Output:
      "Status: 1011"
      
      1. QString::arg()
      2. unsigned char status = 11;
        
        // to print as string with 8 characters padded with '0'
        qDebug() << "Status1:" << QString("%1").arg(status, 8, 2, QChar('0'));
        
        // use noquote() if you do not want to print the quotes
        qDebug().noquote() << "Status2:" << QString("%1").arg(status, 8, 2, QChar('0'));
        
        Output:
        Status1: "00001011"
        Status2: 00001011