PDA

View Full Version : [Qt4] qdatastream/qstring serialization



fabo
19th April 2006, 11:11
hi,

using this sample code :


QString date = QDate::currentDate().toString("dd/MM/yy");
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setByteOrder(QDataStream::LittleEndian);
out.setVersion(QDataStream::Qt_4_0);


* test 1 :


out << date.toAscii(); // size = 12
qDebug() << "size = " << block.size();


* test 2 :


out << qPrintable(date); // size = 13
qDebug() << "size = " << block.size();


* test 3 :


out << date; // size = 20
qDebug() << "size = " << block.size();


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

vratojr
19th April 2006, 12:28
Hi, the informations I use are taken from:
http://doc.trolltech.com/4.1/datastreamformat.html


* test 1 :


out << date.toAscii(); // size = 12
qDebug() << "size = " << block.size();



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.



* test 2 :


out << qPrintable(date); // size = 13
qDebug() << "size = " << block.size();


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...?


* test 3 :


out << date; // size = 20
qDebug() << "size = " << block.size();



QStrings are written in the format quint32+data in UTF16 so,each char is 2 bytes so:
4+nchar*2=16=20.

RaKKeR
19th April 2006, 12:29
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

fabo
19th April 2006, 19:22
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

wysota
19th April 2006, 19:31
Right. You can just write to file (QFile) directly or using QTextStream which doesn't add any additional data.