Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qDebug - How to output data in bits (binary format)

Tags:

c++

qt

qt5

qdebug

Can qDebug() output the data in binary format?
For example, I want to check some status variation:

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

I want to generate output in a binary format, something like:

Status: 1011
like image 964
jingweimo Avatar asked Mar 02 '18 17:03

jingweimo


1 Answers

If you want to print in binary you can use:

  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
like image 80
eyllanesc Avatar answered Nov 09 '22 01:11

eyllanesc