What do you try to achieve? Why do you think that both should be the same? QDataStream has an internal stream format so it adds some bits and bytes to your data so it knows what kind of data it is.
What do you try to achieve? Why do you think that both should be the same? QDataStream has an internal stream format so it adds some bits and bytes to your data so it knows what kind of data it is.
QDataStream is, in general, expecting to be reading a data stream created by another QDataStream. If just want to read raw bytes then use QIODevice directly.
The QDataStream output of a QByteArray contains bytes other than just the content of the array. Consequently, when asked to read a QByteArray it expects those extra bytes. QByteArray serialises the size of the array as a 32-bit integer before the content itself so the receiver can know where the data ends.
Qt Code:
#include <QCoreApplication> #include <QDataStream> #include <QBuffer> #include <QDebug> int main(int argc, char **argv) { // Load a test QByteArray with 8 bytes // Serialise it using QDataStream QBuffer buffer; out << payload; // What went into the buffer? qDebug() << buffer.buffer().toHex(); // Output: "000000083031323334353637" return 0; }To copy to clipboard, switch view to plain text mode
In your example the input hex string is 31 digits (odd) and QByteArray has assumed a leading zero. The data array contains bytes (hex): 02 00 00 00 08 30 53 70 ff ff ff 63 05 00 00 8e
The first four bytes will be consumed as a size (a very big one), and the content will start at 0x08.
On my machine convertedStr is empty, probably because the input stream was exhausted before 0x02000000 bytes could be read.
Thanks, ChrisW67 for a detailed explanation, but I have a different use case.
In my code, I have to pass a QByteArray to a generic API which is taking QDataStream as a parameter. In that API I need to again deserialize the QDataStream to QByteArray.
Something like
Qt Code:
{ //Here I need to read QByteArray from QDataStream QByteArray convertedData; stream>> convertedData; //print converted data. //But the data is coming as empty for me } int main() { readContainer(dataStreamToReadFrom); return 0; }To copy to clipboard, switch view to plain text mode
Then you should create the data via QDataStream. You can't simply throw some random bytes to QDataStream and hope it's properly decoded afterwards.
A QDataStream object is not the data, nor does it contain the data, it merely decodes a set of bytes provided to it. You need to construct a correct set of bytes to provide it.
Assuming you are expecting the "other end" to receive exactly the bytes you have in your code:
Then buffer contains the bytes you need to transmit, andQt Code:
// Serialise it using QDataStream QBuffer buffer; out << payload; buffer.close();To copy to clipboard, switch view to plain text mode
should get the bytes there.Qt Code:
readContainer(passThis); }To copy to clipboard, switch view to plain text mode
Bookmarks