Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用QString和QByteArray的QDataStream存在问题_C++_Qt_Qstring_Qbytearray_Qdatastream - Fatal编程技术网

C++ 使用QString和QByteArray的QDataStream存在问题

C++ 使用QString和QByteArray的QDataStream存在问题,c++,qt,qstring,qbytearray,qdatastream,C++,Qt,Qstring,Qbytearray,Qdatastream,如果QString的大小是1,ba的大小加上6,为什么会这样sizeof(QString)为4。通过检查字符串的存储和检索方式: char*字符串写为32位整数,等于 包含“\0”字节的字符串,后跟 包含“\0”字节的字符串。读取char*字符串时,4 读取字节以创建32位长度值,然后 包含“\0”终止符的char*字符串的字符为 阅读 因此,在您的例子中,字符串长度为32位+1字节表示“a”+1字节表示\0,总计为6字节。让我们分析两种印象之间的差异: QByteArray ba; QData

如果QString的
大小
是1,ba的大小加上6,为什么会这样
sizeof(QString)
为4。

通过检查字符串的存储和检索方式:

char*字符串写为32位整数,等于 包含“\0”字节的字符串,后跟 包含“\0”字节的字符串。读取char*字符串时,4 读取字节以创建32位长度值,然后 包含“\0”终止符的char*字符串的字符为 阅读


因此,在您的例子中,字符串长度为32位+1字节表示“a”+1字节表示\0,总计为6字节。

让我们分析两种印象之间的差异:

QByteArray ba;
QDataStream ds(&ba,QIODevice::WriteOnly);
QString s="a";
ds<<quint8(1)<<quint16(2)<<quint32(3)<<s;  //1+2+4+a
qDebug()<<"size:"<<ba.size();   // 13
为此,让我们回顾一下:

通常,您可以使用以下公式计算存储数据的大小:

      x00\x00\x00\x02          \x00""a
      ---------------          -------
  numbers of bytes of buffer    buffer

谢谢,真的很有帮助,给人留下深刻印象
"\x01\x00\x02\x00\x00\x00\x03"
"\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x02\x00""a"
-----------------------------------------------------
                              x00\x00\x00\x02\x00""a
QDataStream &operator<<(QDataStream &out, const QString &str)
{
    if (out.version() == 1) {
        out << str.toLatin1();
    } else {
        if (!str.isNull() || out.version() < 3) {
            if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
                out.writeBytes(reinterpret_cast<const char *>(str.unicode()), sizeof(QChar) * str.length());
            } else {
                QVarLengthArray<ushort> buffer(str.length());
                const ushort *data = reinterpret_cast<const ushort *>(str.constData());
                for (int i = 0; i < str.length(); i++) {
                    buffer[i] = qbswap(*data);
                    ++data;
                }
                out.writeBytes(reinterpret_cast<const char *>(buffer.data()), sizeof(ushort) * buffer.size());
            }
        } else {
            // write null marker
            out << (quint32)0xffffffff;
        }
    }
    return out;
}
      x00\x00\x00\x02          \x00""a
      ---------------          -------
  numbers of bytes of buffer    buffer
length stored data = 4 + 2 x length of string