gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:33 25 4
gpt4 key购买 nike

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

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

我想生成二进制格式的输出,类似于:

Status: 1011

最佳答案

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

  1. bin
unsigned char status = 11;
qDebug() << "Status:" << bin << status;

Output:
"Status: 1011"
  1. QString::number()
unsigned char status = 11;
qDebug() << "Status:" << QString::number(status, 2);

Output:
"Status: 1011"
  1. QString::arg()
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

关于c++ - qDebug - 如何以位输出数据(二进制格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49074231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com