[Qt-interest] QDataStream writing a quint32 twice

Andre Somers andre at familiesomers.nl
Fri Dec 11 16:06:29 CET 2009


Hi,

Easy enough, and well documented. QString::toAscii() returns a QByteArray.
If you look up what the << operator does for QByteArray, you'll find this:

    Writes byte array /ba/ to the stream /out/ and returns a reference
    to the stream.

    See also Format of the QDataStream operators
    <http://doc.trolltech.com/4.6/datastreamformat.html>.

Looking up the latter reference, you will see the format for QByteArray 
defined as:

QByteArray <qbytearray.html> 	

    * If the byte array is null: 0xFFFFFFFF (quint32)
    * Otherwise: the array size (quint32) followed by the array bytes,
      i.e. size bytes



That means that in your code, you first write out the size yourself, and 
then let QByteArray do it again. It is only logical that you end up with 
two size indicators. See http://doc.trolltech.com/4.6/datastreamformat.html.

André

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
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091211/eef2086c/attachment.html 


More information about the Qt-interest-old mailing list