gpt4 book ai didi

c++ - 将 QByteArray 附加到 QDataStream?

转载 作者:太空狗 更新时间:2023-10-29 19:39:22 24 4
gpt4 key购买 nike

我必须用不同的数据填充QByteArray。所以我正在使用 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

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)

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.

你可以使用writeRawData来规避它:

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

关于c++ - 将 QByteArray 附加到 QDataStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23271029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com