【问题标题】:QBuffer writes bytes at the start of QByteArray rather than the endQBuffer 在 QByteArray 的开头而不是结尾写入字节
【发布时间】:2019-01-20 23:11:52
【问题描述】:

我有以下代码:

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 的小端十六进制值表示,但是,它是在开始处添加的字节数组的而不是结尾。如何追加我的值到字节数组的end

【问题讨论】:

    标签: qt qbytearray qbuffer


    【解决方案1】:

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

    tempBuffer.open(QIODevice::Append);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2016-03-08
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多