Converting string to hex before inserting into QTextEdit
-
wrote on 14 Sept 2022, 06:26 last edited by
Hello. I have been working with my serial terminal program. When I connect to external device, I can read incoming data and print it out on my console without any issues.
I would like to improve my console and allow user to convert all incoming data to hex and print it out to the console. I have made a checkbox and before I insert data to the QTextEdit I check if the checkbox is checked as following:
void Widget::readData() { QByteArray data = serial_local->serial_connection.readAll(); QString DataAsString = QString(data); //handle data in hex format if(ui->hex_checkbox->isChecked()) { QByteArray data_to_transmit = QByteArray::fromHex(data); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); } //handle data in string format else { DataAsString.replace("\r", "\n"); ui->Console_read->insertPlainText(DataAsString); } }
As you can see from above, I have tried the following to covert data to hex as following:
if(ui->hex_checkbox->isChecked()){ QByteArray data_to_transmit = QByteArray::fromHex(data); //QString res = DataAsString.toUtf8().toHex(); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); }
I can see on the application output:
"\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
It is quite strange because the external device that I am connected to is sending me the following data:
while(1){ printf("hello message1 \n"); printf("hello message2 \n"); delay(1000); printf("new message1 \r"); printf("new message2 \r"); delay(1000); }
So I am expecting to receive the following data:
68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 31 0A // hello message1 68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 32 0A //hello message2 6E 65 77 20 6D 65 73 73 61 67 65 31 0A // new message1 6E 65 77 20 6D 65 73 73 61 67 65 32 0A // new message1
-
Hello. I have been working with my serial terminal program. When I connect to external device, I can read incoming data and print it out on my console without any issues.
I would like to improve my console and allow user to convert all incoming data to hex and print it out to the console. I have made a checkbox and before I insert data to the QTextEdit I check if the checkbox is checked as following:
void Widget::readData() { QByteArray data = serial_local->serial_connection.readAll(); QString DataAsString = QString(data); //handle data in hex format if(ui->hex_checkbox->isChecked()) { QByteArray data_to_transmit = QByteArray::fromHex(data); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); } //handle data in string format else { DataAsString.replace("\r", "\n"); ui->Console_read->insertPlainText(DataAsString); } }
As you can see from above, I have tried the following to covert data to hex as following:
if(ui->hex_checkbox->isChecked()){ QByteArray data_to_transmit = QByteArray::fromHex(data); //QString res = DataAsString.toUtf8().toHex(); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); }
I can see on the application output:
"\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2" "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
but in my console I see:
It is quite strange because the external device that I am connected to is sending me the following data:
while(1){ printf("hello message1 \n"); printf("hello message2 \n"); delay(1000); printf("new message1 \r"); printf("new message2 \r"); delay(1000); }
So I am expecting to receive the following data:
68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 31 0A // hello message1 68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 32 0A //hello message2 6E 65 77 20 6D 65 73 73 61 67 65 31 0A // new message1 6E 65 77 20 6D 65 73 73 61 67 65 32 0A // new message1
@lukutis222 QByteArray::fromHex != toHex :D
-
@lukutis222 QByteArray::fromHex != toHex :D
wrote on 14 Sept 2022, 06:52 last edited by lukutis222@J-Hilk
Thanks for response. I am very confused. Is this an error in QT documentation?It talks about toHex but then in the example it uses fromHex
Regardless of this, I think I have managed to get it to work:
QByteArray data = serial_local->serial_connection.readAll(); QString DataAsString = QString(data); if(ui->hex_checkbox->isChecked()){ QByteArray data_to_transmit = data.toHex(); //QString res = DataAsString.toUtf8().toHex(); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); }
Thank you
-
@J-Hilk
Thanks for response. I am very confused. Is this an error in QT documentation?It talks about toHex but then in the example it uses fromHex
Regardless of this, I think I have managed to get it to work:
QByteArray data = serial_local->serial_connection.readAll(); QString DataAsString = QString(data); if(ui->hex_checkbox->isChecked()){ QByteArray data_to_transmit = data.toHex(); //QString res = DataAsString.toUtf8().toHex(); qDebug() << data_to_transmit; ui->Console_read->insertHtml(data_to_transmit); }
Thank you
@lukutis222 said in Converting string to hex before inserting into QTextEdit:
Is this an error in QT documentation?
It talks about toHex but then in the example it uses fromHexno, not really. The example uses fromHex in the first line to fill the data of the QByteArray, which is internally a char array.
QByteArray does currently not support initialiser list construction, so the documentation uses the fromHex(string) static function to create and fill the QByteArray in one line
1/4