How to add an arbitrary number of null bytes into a qbytearray upon construction the right way?
Solved
General and Desktop
-
I am trying to construct a
QByteArray
which contains null bytes. Let's say two null bytes surorunded by blanks.So far I only managed it to do via
QByteArray::fromHex("323200003232")
.
Is there a way, doing it with one of the class constructors? Things likeQByteArray("\x32\x32\x00\x00\x32\x32")
fail, because the null bytes are obviously seen as being invalid null characters and therefore are cut out. -
@devjb
Use the overloadQByteArray::QByteArray(const char *data, int size = -1)
(https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1) or[static]QByteArray QByteArray::fromRawData(const char *data, int size)
(https://doc.qt.io/qt-5/qbytearray.html#fromRawData) and specifysize
(e.g.sizeof()
) as the correct length of your data.