[Qt-interest] QDataStream writing a quint32 twice
Volker
volker at openbios.org
Fri Dec 11 15:54:25 CET 2009
On Friday 11 December 2009 15:43:47 Rohan Prabhu wrote:
> This is a rather strange problem I'm facing. Have a look at this code:
>
> QDataStream out(&block, QIODevice::WriteOnly);
> QString newString("Hello.World.This.is.the.message");
> QByteArray newByte = newString.toAscii();
>
> //Send to stream the magic words
> out<<(quint8)'x';
> out<<(quint8)0x37;
> out<<(quint8)0x4B;
>
> //Send to stream the version numbers
> out<<(quint8)0x00;
> out<<(quint8)0x01;
>
> //Send to stream the size of the request
> [LINE] out<<(quint32)((newString.toAscii()).size());
>
> //Send to stream the data
> out<<newString.toAscii();
>
> std::cout<<std::endl;
>
> Now, the contents of the QByteArray 'block' are:
>
> 0x78 0x37 0x4b 0x0 0x1 *0x0 0x0 0x0 0x1f 0x0
> 0x0 0x0 0x1f* 0x48 0x65 0x6c 0x6c 0x6f 0x2e 0x57
> 0x6f 0x72 0x6c 0x64 0x2e 0x54 0x68 0x69 0x73 0x2e
> 0x69 0x73 0x2e 0x74 0x68 0x65 0x2e 0x6d 0x65 0x73
> 0x73 0x61 0x67 0x65
>
> The bold and underlined text is the 'size' variable, the line of output is
> mentioned by the prefix [LINE] in the code. The size of the string is 31
> bytes [which is 0x1F bytes]. And 0x48, 0x65 and 0x6C are the hex codes for
> 'H', 'e' and 'l' respectively. So, basically, it is clear that the size
> variable is being written twice. Now I dont quite understand why. I made
> the variable newByte to test and found out it's contents. The contents
> started at 0x48 0x65 and so on.. So basically, the ByteArray construction
> is proper via QString::toAscii(), but for some reason, the size variable
> is being inserted twice.
>
> Any help would be appreciated,
> Regards,
> Rohan Prabhu
>
The second size field comes from the QByteArray operator<<()
See doc/html/datastreamformat.html
Regards
.....Volker
More information about the Qt-interest-old
mailing list