Qt QString add null terminator
-
wrote on 8 Jan 2020, 00:25 last edited by
How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.
-
How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.
Hi, and welcome!
@ConductedForce said in Qt QString add null terminator:
How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.
In ASCII, the null-terminator is the null character, 0x00. It is represented as the
'\0'
literal in C/C++.Note: Technically, you send a byte array across the network; you don't send a text string.
First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.
-
Hi, and welcome!
@ConductedForce said in Qt QString add null terminator:
How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.
In ASCII, the null-terminator is the null character, 0x00. It is represented as the
'\0'
literal in C/C++.Note: Technically, you send a byte array across the network; you don't send a text string.
First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.
wrote on 8 Jan 2020, 08:17 last edited by JonB 1 Aug 2020, 08:21@JKSH
Since this topic is marked as solved now anyway, I feel like observing:First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.
Actually, according to me, you could skip appending that
\0
character. When you have aQByteArray
,QByteArray::data()
is (surprisingly) always NUL-terminated, with the\0
being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually sendQByteArray::size() + 1
bytes fromdata()
and save on extending the byte array :) -
@JKSH
Since this topic is marked as solved now anyway, I feel like observing:First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.
Actually, according to me, you could skip appending that
\0
character. When you have aQByteArray
,QByteArray::data()
is (surprisingly) always NUL-terminated, with the\0
being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually sendQByteArray::size() + 1
bytes fromdata()
and save on extending the byte array :)@JonB said in Qt QString add null terminator:
you could skip appending that
\0
character. When you have aQByteArray
,QByteArray::data()
is (surprisingly) always NUL-terminated, with the\0
being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually sendQByteArray::size() + 1
bytes fromdata()
and save on extending the byte array :)Good catch!
1/4