Quote Originally Posted by alokraj001 View Post
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.
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:
Qt Code:
  1. QByteArray payload = QByteArray::fromHex("0200000008305370FFFFFF630500008E");
  2. // Serialise it using QDataStream
  3. QBuffer buffer;
  4. buffer.open(QIODevice::WriteOnly);
  5. QDataStream out(&buffer);
  6. out << payload;
  7. buffer.close();
To copy to clipboard, switch view to plain text mode 
Then buffer contains the bytes you need to transmit, and
Qt Code:
  1. if (buffer.open(QIODevice::ReadOnly)) {
  2. QDataStream passThis(&buffer);
  3. readContainer(passThis);
  4. }
To copy to clipboard, switch view to plain text mode 
should get the bytes there.