简体   繁体   English

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

[英]QBuffer writes bytes at the start of QByteArray rather than the end

I have the following code: 我有以下代码:

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

When I print to console the content of my byte array: 当我打印以控制台字节数组的内容时:

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

The console prints: 控制台显示:

ba:  "0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

As can be seen above, the 2 which is of type quint32 , is correctly represented by the little Endian hex value of 0x02000000 , however, it is added at the start of the byte array rather than the end. 从上面可以看出,类型为quint322正确地由小Endian十六进制值0x02000000 ,但是,它是在字节数组的开始而不是结尾处添加的。 How can I append my value to the end of the byte array? 如何我的值附加到字节数组的末尾

追加模式而不是writeonly中打开缓冲区:

tempBuffer.open(QIODevice::Append);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM