[Qt-interest] QDataStream writing a quint32 twice

Thiago Macieira thiago at kde.org
Fri Dec 11 16:01:04 CET 2009


Em Sexta-feira 11 Dezembro 2009, às 15:43:47, Rohan Prabhu escreveu:
> //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.

Hi Rohan

You forgot the fact that QDataStream inserts the size when it gets the string. 
QDataStream is its own protocol, it's not just pure binary data.

This is documented in the QDataStream protocol:
http://doc.trolltech.com/4.6/datastreamformat.html

"const char *:
  - The string length (quint32)
  - The string bytes, excluding the terminating 0"

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091211/526317a7/attachment.bin 


More information about the Qt-interest-old mailing list