将QByteArray附加到QDataStream吗? [英] Append a QByteArray to QDataStream?

查看:305
本文介绍了将QByteArray附加到QDataStream吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用不同的数据填充QByteArray.所以我正在使用QDataStream.

I have to populate a QByteArray with different data. So I'm using the QDataStream.

QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);

qint8 dataHex= 0x04;
qint8 dataChar = 'V';

stream << dataHex<< dataChar;
qDebug() << buffer.toHex();  // "0456"  This is what I want

但是,我还想将QByteArray附加到buffer.

However, I would also like to append a QByteArray to the buffer.

QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);

qint8 dataHex= 0x04;
qint8 dataChar = 'V';
QByteArray moreData = QByteArray::fromHex("ff");

stream << dataHex<< dataChar << moreData.data(); // char * QByteArray::data ()
qDebug() << buffer.toHex();  // "045600000002ff00"  I would like "0456ff"

我想念什么?

推荐答案

在附加char*时,它假定\0终止并用writeBytes进行序列化,该序列也首先写出大小(如uint32)

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

writeBytes'文档:

将长度说明符len和缓冲区s写入流中,并 返回对该流的引用.

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

len被序列化为quint32,后跟来自s的len个字节.笔记 数据未编码.

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

您可以使用writeRawData来规避它:

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

这篇关于将QByteArray附加到QDataStream吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆