QBuffer在QByteArray的开头而不是结尾处写入字节

时间:2018-08-14 07:06:52

标签: qt qbytearray qbuffer

我有以下代码:

QByteArray ba; // Declare Byte array
ba.clear();    // Clear it
ba.append(80, 0x00); // Append 80 bytes of 0x00
quint32 Count = 2;   // The number we want to append to the byte array
QBuffer tempBuffer(&ba); // We use temporary buffer to conveniently put integers and floats into byte-array
tempBuffer.open(QIODevice::WriteOnly);
Count = qToLittleEndian(Count); // Make sure our number is little Endian
tempBuffer.write((char*)&Count, sizeof(quint32)); // Write the number to byte array

当我进行打印以控制字节数组的内容时:

qDebug() << "ba: " << ba.toHex();

控制台显示:

ba:  "0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

从上面可以看出,2类型的quint320x02000000的小Endian十六进制值正确表示,但是它被添加到开始,而不是结束。我如何附加我的值到字节数组的结尾

1 个答案:

答案 0 :(得分:2)

append 模式而不是 writeonly 打开缓冲区:

tempBuffer.open(QIODevice::Append);