[Qt-interest] QDataStream writing a quint32 twice
Rohan Prabhu
rohan at rohanprabhu.com
Fri Dec 11 16:23:00 CET 2009
Thanks a lot everybody for helping me figuring this out. Guess I should
consult the Qt documentation more often. However, in subsequent releases,
the size field wont be adjacent to the data header. Hence, just using '
out<<newString.toAscii() ' wont help. I need to write the 'size' first:
out<<someByteArray.size();
out<<(quint32)someData;
//Write data from someByteArray to QDataStream.
How do I go about it now, without writing the size of ' someByteArray ' to
QDataStream?
Any help is appreciated,
Regards,
Rohan Prabhu
On Fri, Dec 11, 2009 at 8:24 PM, Volker <volker at openbios.org> wrote:
> 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
>
> _______________________________________________
> 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/5405fb49/attachment.html
More information about the Qt-interest-old
mailing list