LineEdit widget text to QByteArray (\n to 0x0A)
how to conver QString from LineEdit->text() to QByteArray? \n must be converted to byte 0x0A.
Hi! Depends on the encoding you need in the resulting byte array. E.g., to get a UTF-8 encoded string, you'd use
QByteArray QString::toUtf8() const.
data_to_send = ui->lineEdit_send->text().toUtf8();
and I get two bytes 0x5C 0x6E in byte array when I write in line edit \n
but I need 0x0A
You could e.g. replace the 2-character-sequence of "\n" in the string before you convert it to a byte array:
data_to_send = QString( ui->lineEdit_send->text() ).replace("\\n", "\n").toUtf8();
It works, and what about other syntax like \r CR and etc? its possible to convert all of them to hex correctly?
Sure, just repeat the code from my last post for all sequences you want to replace. A list of the escape sequences used in C++ can be found here. As a side-note, you could also use
QString::replace(). Or you could replace the characters in the byte array, using
I tryed to use: data_to_send = ui->lineEdit_send->text().replace( "\\" , "\" ).toUtf8();
but the end of this line seems to be commented because there is ""
.replace( "\" , "" )is an error in your source code, because \" is the escape sequence for double quote. Have a look at http://en.cppreference.com/w/cpp/language/escape to see how escape sequences in C++ work.
Wouldn't QByteArray::toHex fill the bill ?
OK. I used this method: