QBuffer 在 QByteArray 的开头而不是结尾写入字节
QBuffer writes bytes at the start of QByteArray rather than the end
我有以下代码:
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"
如上所示,quint32
类型的 2
正确表示为 0x02000000
的小端十六进制值,但是,它被添加到字节数组的开始而不是结尾。我如何 append 我的值到字节数组的 end?
以append模式而不是writeonly打开缓冲区:
tempBuffer.open(QIODevice::Append);
我有以下代码:
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"
如上所示,quint32
类型的 2
正确表示为 0x02000000
的小端十六进制值,但是,它被添加到字节数组的开始而不是结尾。我如何 append 我的值到字节数组的 end?
以append模式而不是writeonly打开缓冲区:
tempBuffer.open(QIODevice::Append);