How to edit output of QByteArray?
-
Hello everyone,
QByteArray which is "response" in this case and my function as follows:void checkVersion() { QByteArray response; QByteArray command = QByteArray::fromHex("04A30400FF55"); m_serialPort->write(command); m_serialPort->waitForBytesWritten(500); while(m_serialPort->waitForReadyRead(500)) { response.append(m_serialPort->readAll()); } qDebug() << response; }
When I qDebug() this "response", the output is like this:
"H\xA4\x00\x00Product Name: Product ID: Hardware version: Firmware version:1.13.41\xE7\xAF"
And my desire output is:
"Product Name: Product ID: Hardware version: Firmware version:1.13.41"
Which function should I use or which subject should I research?
Thanks1@eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray -
@eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray -
@eefesafak Do you have a protocol for this communication? How do you send the data? Do you use https://doc.qt.io/qt-6/qdatastream.html to send data?
And didn't you already ask exact same question in the past? https://forum.qt.io/topic/137606/how-to-delete-hex-numbers-from-qbytearray -
@eefesafak said in How to edit output of QByteArray?:
Communication is via QSerialPort.
This is not what I asked.
I asked:- Do you have a photocall?
- How exactly do you send data? Do you use QDataStream?
The question I want to know is: why do you get all these bytes (H\xA4\x00\x00) - you should know that as you send the data, right?
-
Hello everyone,
QByteArray which is "response" in this case and my function as follows:void checkVersion() { QByteArray response; QByteArray command = QByteArray::fromHex("04A30400FF55"); m_serialPort->write(command); m_serialPort->waitForBytesWritten(500); while(m_serialPort->waitForReadyRead(500)) { response.append(m_serialPort->readAll()); } qDebug() << response; }
When I qDebug() this "response", the output is like this:
"H\xA4\x00\x00Product Name: Product ID: Hardware version: Firmware version:1.13.41\xE7\xAF"
And my desire output is:
"Product Name: Product ID: Hardware version: Firmware version:1.13.41"
Which function should I use or which subject should I research?
Thanks1@eefesafak so, you only want the printable part:
QString qBytearrayToCleanedUpString( const QByteArray &data) { QString returnStr; for(auto d : data){ if(QChar c(d); c.isPrint()){ returnStr.append(c); } } return returnStr; }
-
@eefesafak said in How to edit output of QByteArray?:
Communication is via QSerialPort.
This is not what I asked.
I asked:- Do you have a photocall?
- How exactly do you send data? Do you use QDataStream?
The question I want to know is: why do you get all these bytes (H\xA4\x00\x00) - you should know that as you send the data, right?
-
@jsulm I am working with qr reader. In the document of the Qr company there was a hexadecimal operation instruction that allowed us to check the software version. I sent it to Qr with Serialport. Briefly, it returns the software version.
@eefesafak Apparently it returns more than just the version. Isn't there any documentation describing what exactly is returned? Or how the protocol looks like? Do you know why there are "H\xA4\x00\x00" prefix and "\xE7\xAF" suffix? Is this part of any data package you get?
You could simply delete these bytes (assuming you always get 4 bytes at the beginning and 2 bytes at the end of the response which you do not need):response = response.sliced(0, 4); // remove first 4 bytes response = response.truncate(2); // remove last two bytes
-
@eefesafak The question is about the protocol. Yes, the device sends a string of characters containing a human-readable version, but it also send the other bytes. The device is not sending random response bytes, they probably mean something. There should be documentation about what those other bytes mean, just like the documentation that tells you what bytes to send to make a request. Rather than blindly removing the non-printable bytes you do not understand (which will not remove the leading H) you could try to understand what is in the response and code intelligently.
For example, the first byte 'H' is the number 72, which looks roughly like the number of bytes in the remainder of the message.
-
@eefesafak so, you only want the printable part:
QString qBytearrayToCleanedUpString( const QByteArray &data) { QString returnStr; for(auto d : data){ if(QChar c(d); c.isPrint()){ returnStr.append(c); } } return returnStr; }
-
@J-Hilk Could you explain, what does it use for:
if(QChar c(d); c.isPrint()){ returnStr.append(c); }
If you do not mind?
@eefesafak its a shortened for loop over a container, Qt previously had Q_FOREACH for that, but the c++ standard now has this for loop version.
In this case it does go through every "byte" of the QByteArray - from beginning to end - takes that char, converts it to a QChar and then checks if its a printable character. If thats true, it appends the QChar to the local QString variable
see there:
https://en.cppreference.com/w/cpp/language/iffor more info about the used "If statements with initializer"
-
@eefesafak its a shortened for loop over a container, Qt previously had Q_FOREACH for that, but the c++ standard now has this for loop version.
In this case it does go through every "byte" of the QByteArray - from beginning to end - takes that char, converts it to a QChar and then checks if its a printable character. If thats true, it appends the QChar to the local QString variable
see there:
https://en.cppreference.com/w/cpp/language/iffor more info about the used "If statements with initializer"