Results 1 to 5 of 5

Thread: [Qt4] qdatastream/qstring serialization

  1. #1
    Join Date
    Apr 2006
    Posts
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default [Qt4] qdatastream/qstring serialization

    hi,

    using this sample code :

    Qt Code:
    1. QString date = QDate::currentDate().toString("dd/MM/yy");
    2. QByteArray block;
    3. QDataStream out(&block, QIODevice::WriteOnly);
    4. out.setByteOrder(QDataStream::LittleEndian);
    5. out.setVersion(QDataStream::Qt_4_0);
    To copy to clipboard, switch view to plain text mode 

    * test 1 :
    Qt Code:
    1. out << date.toAscii(); // size = 12
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 

    * test 2 :
    Qt Code:
    1. out << qPrintable(date); // size = 13
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 

    * test 3 :
    Qt Code:
    1. out << date; // size = 20
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 

    i expected to find (or would find) size = 8 like :
    block[0] = 'd'
    block[1] = 'd'
    block[2] = '/'
    block[3] = 'M'
    block[4] = 'M'
    block[5] = '/'
    block[6] = 'y'
    block[7] = 'y'

    any clarifications for me ?

    cheers,

    Fathi

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] qdatastream/qstring serialization

    Hi, the informations I use are taken from:
    http://doc.trolltech.com/4.1/datastreamformat.html
    Quote Originally Posted by fabo
    * test 1 :
    Qt Code:
    1. out << date.toAscii(); // size = 12
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 
    the toAscii() returns a QByteArray. When serialized to a DataStream, the QByteArray format is quint32+array bytes so:
    4+dd/MM/yy(=8chars)=12 chars.

    Quote Originally Posted by fabo
    * test 2 :
    Qt Code:
    1. out << qPrintable(date); // size = 13
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 
    from:
    http://doc.trolltech.com/4.1/qdatastream.html
    To take one example, a char * string is written as a 32-bit integer equal to the length of the string including the '\0' byte, followed by all the characters of the string including the '\0' byte. When reading a char * string, 4 bytes are read to create the 32-bit length value, then that many characters for the char * string including the '\0' terminator are read.
    So,it's the same thing a the above situation but you have to consider the \0 byte.
    It's true that in the first link it's written that const char* are written without the \0 byte but.. Maybe it's a typo...?
    Quote Originally Posted by fabo
    * test 3 :
    Qt Code:
    1. out << date; // size = 20
    2. qDebug() << "size = " << block.size();
    To copy to clipboard, switch view to plain text mode 
    QStrings are written in the format quint32+data in UTF16 so,each char is 2 bytes so:
    4+nchar*2=16=20.

  3. #3
    Join Date
    Jan 2006
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] qdatastream/qstring serialization

    Dear fabo,

    The number of bytes that goes into the byte array depends on what you put in it:

    in test1 you put a string of 8 characters in it and QDataStream prepends this with an integer (=4bytes) to remember the size of the data ==> 12 bytes

    in test2 QDataStream writes the null terminated ('\0' == extra byte) string to the bytearray again prepended with an integer telling the size of this data. ==> 13 bytes

    in test3 QDataStream seems to need 20bytes to store a QString object

    hope this helps,

    Greetz,

    RaKKeR

  4. #4
    Join Date
    Apr 2006
    Posts
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] qdatastream/qstring serialization

    thanks for the reply.

    As i understand, if i want only the relevant data ("dd/MM/yy") in my qbytearray :
    1) i must use QDataStream::writeRawData()
    or
    2) avoid using qdatastream (serialization) and only use qbytearray

    right ?

    cheers,

    Fathi

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Qt4] qdatastream/qstring serialization

    Right. You can just write to file (QFile) directly or using QTextStream which doesn't add any additional data.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.