listing values in QByte
-
Hi,
I followed a stackoverflow answer to a problem but it gives me an error instead, is there anything i missed like an include?The answer i followed :
The same happens with an unsigned char, so i kept it as a QByteArray.
void MainWindow::writeSerial() { QByteArray message; message.append(tester) ; qDebug() << "binary" << bin << message ; if (m_serial->open(QIODevice::ReadWrite)) { m_serial->write(message); m_serial->waitForBytesWritten(-1); m_serial->close(); } }
error: ‘bin’ was not declared in this scope; did you mean ‘Qt::bin’?
116 | qDebug() << "binary" << bin << message ;
| ^~~
| Qt::binchanging it to Qt::bin does not help either...
-
@agmar OK, I misunderstood you, sorry!
I think you see this difference because 0xAA is extended ASCII and I guess qDebug() prints its hex value, 0x55 is ASCII and qDebug() prints it as ASCII character.
What you should try: do not print the byte array as a whole, print each byte:qDebug() << Qt::bin << message[0];
-
@agmar The error actually tells you exactly what the problem is.
Please don't just copy/paste some code and report problems. Think about what you're doing.
What is bin supposed to be in your case?Also, what problem are you actually trying to solve?
-
I just wanted to represent the values stored in the QByteArray through qDebug() in binary format to see if the message is what i expect it to be...
And like in the SO answer, they use bin , which i in turn expect to have the same functionality in my version of the implementation, but it does not, so any other way to do it would be good -
@jsulm #include <Qt> für Qt::bin see https://doc.qt.io/qt-6/qtextstream.html#qtextstream-manipulators
-
Works fine for me
#include <QCoreApplication> int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); int tmp = 42; qDebug() << Qt::bin << tmp; return 0; }
-
is there anyway to do it with a QByteArray? it does not give me the output i expect
void MainWindow::writeSerial() { QByteArray message; message.append(tester) ; qDebug() << "binary" << Qt::bin << tester ; qDebug() << "supposed to be binary" << Qt::bin << message; if (m_serial->open(QIODevice::ReadWrite)) { m_serial->write(message); m_serial->waitForBytesWritten(-1); m_serial->close(); } }
output:
binary 10101010
supposed to be binary "\xAA"the problem is that if i send 0xAA through the port, it fails to trigger a green LED on my MCU, it responds to 0xFF by toggling a green LED , but the same should happen with 0xAA , with 0xFF the LED functions as expected, but 0xAA has no effect.
both 0xFF and 0xAA succeed in toggling the green LED if used with a terminal like Hterm or Teraterm.
Thats why i want to see whats inside the QByteArray after it has the message inside
-
@agmar said in listing values in QByte:
output:
binary 10101010
supposed to be binary "\xAA"10101010
is binary, just as you asked for!\xAA
is hex, not binary. So did you try, guess what,Qt::hex
? All documented in https://doc.qt.io/qt-6/qt.html. -
I set the value of ''tester'' in hex to begin with , so i do not need to see the hex value inside the QByteArray, i need the binary value of the message once it has the "tester" value appended.
There seems to be something fishy going on with that array because of the following output:
binary 1010101
supposed to be binary "U"when i change the value of tester to 0x55, which should have the opposite bits of 0xAA , why is there an U in there and why is the first 0 of the binary not there?
put simply, i wish to see the binary value of the QByteArray in binary once it has the unsigned char tester appended...
-
@agmar
I don't know what you are asking/thinking, and I don't think anyone else will.binary 1010101
supposed to be binary "U"
Binary is digits in the range 0-1. Just as hex is digits in the range 0-F. There is no such thing as
binary "U"
. "why is the first 0 of the binary not there?" what "first 0"? Do you write, say, the decimal number99
as099
? No, why would you show leading zeroes?There is nothing "fishy" going on.
Oh, 0x55 might be the value for letter
U
?qDebug()
puts interpretation on how to show the characters in aQByteArray
. If you want the content of one shown as hex digits why not use QByteArray QByteArray::toHex(char separator = '\0') const, maybe that is what you are looking for? -
@agmar said in listing values in QByte:
when i change the value of tester to 0x55, which should have the opposite bits of 0xAA , why is there an U in there
Because 0x55 is ASCII representation for character U (https://www.asciitable.com).
Please learn how computers represent data."i wish to see the binary value of the QByteArray in binary once it has the unsigned char tester appended" - if you want to see binary (1010...) then print binary as you already do with qDebug() << "binary" << Qt::bin << tester ;
-
@jsulm
binary 1010101
supposed to be binary "U"binary 11111111
supposed to be binary "\xFF"i know how computers represent data, but do you notice that the same code makes the computer decide that sometimes it gives an U when the stored value is 0x55 and the actual hex value when i replace tester with 0xFF? if it is consistent, it should give an y with 2 dots , also , the 0 in the front is missing on the 0x55 to binary conversion, that is why i want to know the value inside the array after it has the value of tested appended, not the value i append, because i already know it is correct...
I am not sure, this might be because i used an unsigned char for the tester value?
-
@agmar
You are simply making assumptions about howqDebug()
displays things to the user which are not the case.
Both I & @mpergand have now told you what to use for the representation you desire, so why not try it?P.S.
makes the computer decide that sometimes it gives an U when the stored value is 0x55 and the actual hex value when i replace tester with 0xFF?
If you really want to know:
qDebug() << QByteArray
will show those bytes which have an ASCII printable character (0x20--0x7F) as characters (e.g. 0x55 ->U
) and those that don't in hex (e.g. 0xFF ->0xFF
). This is (supposed) to help the user becauseQByteArray
is used in many places where it actually holds printable characters and Qt has a bit of schizophrenia over whetherQByteArray
is really being used to hold characters rather than arbitrary binary bytes (e.g. see how it appends a\0
to data stored in it). -
@agmar said in listing values in QByte:
supposed to be binary "U"
What does this mean?!
As already explained "U" is NOT binary! It is also NOT a valid hex digit.1010101 - is 0x55 in hex, not U
-
@jsulm
fig1
this is what i meant.
fig2i did not imply that U was hex, not binary, the debug comment clearly says "supposed to be binary" indicating that it is not .
My confusion arises here, how come qDebug(), with the exact same code gives :
-
in fig 1, a HEX representation, when the value inside tester is 0xAA
-
in fig2, an ASCII character representation of the hex value when tester is 0x55
Hopefully more clear now. again, this is the same code in both instances, with only the value of tester changed...
-
-
@agmar said in listing values in QByte:
My confusion arises here, how come qDebug(), with the exact same code gives :
in fig 1, a HEX representation, when the value inside tester is 0xAA
in fig2, an ASCII character representation of the hex value when tester is 0x55
Did you read my previous answer where I explained just this?
Have you also appreciated that you ask about (a) the representation of a
quint8
and (b) the representation of a byte in aQByteArray
, and assumeqDebug()
will output them the same, but it does not.Finally you now know what you need to use to get what you desire (always hex representation, right?), don't you? So not sure what you are asking now?
-
@agmar OK, I misunderstood you, sorry!
I think you see this difference because 0xAA is extended ASCII and I guess qDebug() prints its hex value, 0x55 is ASCII and qDebug() prints it as ASCII character.
What you should try: do not print the byte array as a whole, print each byte:qDebug() << Qt::bin << message[0];