Append a QByteArray to QDataStream?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 14:08:02

when a char* is appended it assumes \0 termination and serializes with writeBytes which also writes out the size first (as uint32)

writeBytes' doc:

Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.

The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded.

you can use writeRawData to circumvent it:

stream << dataHex<< dataChar;
stream.writeRawData(moreData.data(), moreDate.size());

The 00000002 is the size of the char array, which is written to the stream.

What you are missing is, QDataStream is not raw data. It has its own simple serialization format. It is most suitable for use cases where data is both written (serialized) and read back (deserialized) with QDataStream, and using a reliable QIODevice (QBuffer or QFile for example).

If you want to add raw data to a QBuffer, you could use a suitable overload of write method. But then you might as well just append to the QByteArray directly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!