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

1

7

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
Sticktight answered 2/3, 2018 at 17:23 Comment(5)
What do you mean "output in a binary format"? With the code you show, what would the output be?Hortenciahortensa
My guess is binary representation.Zendah
@bipll: you got my point!Sticktight
So if status is 0b10101111 you want the output to be "Status: 10101111"?Hortenciahortensa
Possible duplicate of how to output an int in binary?Zendah
H
14

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
Hartz answered 2/3, 2018 at 17:31 Comment(2)
how to set the number of bits for ouput? My program gives 8-bit output not four bitsSticktight
@jingweimo Check the last method, that's the one you want.Hartz

© 2022 - 2024 — McMap. All rights reserved.