LineEdit widget text to QByteArray (\n to 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
. -
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();
-
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
QRegularExpression
instead ofQString::replace()
. Or you could replace the characters in the byte array, usingQByteArray::replace()
. -
.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. -
Hi,
Wouldn't QByteArray::toHex fill the bill ?