Results 1 to 4 of 4

Thread: reading wrong data from QByteArray

  1. #1
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1

    Default Re: reading wrong data from QByteArray

    Hi all,

    This is short code , which is not giving me what I expect. I dump two integers ( 4 , 8 ) in a QByteArray and when I read them with QDataStream I am getting unexpected values -- 67108864 , 134217728.
    What am I doing wrong here?...

    Qt Code:
    1. int numPts = 4;
    2. int numCells = 8;
    3. QByteArray array;
    4.  
    5. int sizeOfChar = sizeof( char );
    6. int sizeOfInt = sizeof( int );
    7.  
    8. array.append( QByteArray::fromRawData ( reinterpret_cast<const char*>( &numPts ), sizeOfInt / sizeOfChar ) );
    9. array.append( QByteArray::fromRawData ( reinterpret_cast<const char*>( &numCells ), sizeOfInt / sizeOfChar ) );
    10.  
    11. QDataStream streamOut( &array , QIODevice::ReadOnly );
    12.  
    13. int readNumPts , readNumCells;
    14.  
    15. streamOut >> readNumPts >> readNumCells;
    16.  
    17. std::cout<<readNumPts<<" "<<readNumCells<<std::endl;
    To copy to clipboard, switch view to plain text mode 


    Regards
    Avanindra Singh

    I still don't know the reason for the above code not working , but writing data with QDataStream into QByteArray gave correct results.

    Qt Code:
    1. int numPts = 4;
    2. int numCells = 8;
    3.  
    4. QByteArray array;
    5.  
    6. int sizeOfChar = sizeof( char );
    7. int sizeOfInt = sizeof( int );
    8.  
    9.  
    10. QDataStream streamIn( &array , QIODevice::WriteOnly );
    11.  
    12. streamIn << numPts << numCells;
    13.  
    14. QDataStream streamOut( &array , QIODevice::ReadOnly );
    15.  
    16. int readNumPts , readNumCells;
    17.  
    18. streamOut >> readNumPts >> readNumCells;
    To copy to clipboard, switch view to plain text mode 
    Last edited by avanindra; 22nd February 2013 at 15:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: reading wrong data from QByteArray

    QDataStream is not a general purpose binary stream. It is a serialization mechanism.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: reading wrong data from QByteArray

    Two words; byte order.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading wrong data from QByteArray

    Quote Originally Posted by ChrisW67 View Post
    Two words; byte order.
    one word: Endianness
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. qbytearray qdatastream qfile writing/reading
    By pekal in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2012, 21:51
  2. Replies: 3
    Last Post: 29th April 2010, 19:11
  3. QextSerialPort reading error: wrong values
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 6th May 2009, 19:29
  4. Reading from QByteArray
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 16:17
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.