Results 1 to 4 of 4

Thread: QDataStream writing wrong values

  1. #1
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default QDataStream writing wrong values

    Hi

    I wish to write something onto a data file. I thought of using QDataStream, but it tens to give me wrong output.

    Qt Code:
    1. QFile file("file.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4. out.setByteOrder(QDataStream::LittleEndian);
    5. //out << QString("the answer is"); // serialize a string
    6. int x = 54;
    7. out << (qint32)x;
    8. out.device()->close();
    To copy to clipboard, switch view to plain text mode 

    I was expecting an output of 54. Itself it shouwing me its char value = 6; even with other numbers.

    Please let me know how to address this problem

    Thanks

    Regards
    Alok
    Last edited by alok9871; 21st March 2013 at 03:25.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream writing wrong values

    If you need text output you should use http://qt-project.org/doc/qt-4.8/qtextstream.html instead.

    QDataStream outputs data as sequence of bytes, not printable characters.

  3. The following user says thank you to lanz for this useful post:

    alok9871 (21st March 2013)

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

    Default Re: QDataStream writing wrong values

    Please let me know how to address this problem
    There is no problem. QDataStream is outputting exactly what you asked it to. You have a 32-bit unsigned integer with the value 54 and you have asked for output in LittleEndian order. The integer is 4 bytes 0x00000036, and QDataStream outputs them in reverse order. You get 4 bytes in the file, the first is 54 decimal (0x36) followed by 3 zero bytes. Treated as text this is equivalent to the C string "6".

    Qt Code:
    1. const int num = 54;
    2.  
    3. QFile out("out.txt");
    4. if (out.open(QIODevice::WriteOnly) {
    5. QTextStream s(&out);
    6. s << num;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Also look at QString::number(), QByteArray::number() etc.

  5. The following user says thank you to ChrisW67 for this useful post:

    alok9871 (21st March 2013)

  6. #4
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QDataStream writing wrong values

    Thank you both for your amazing answers. Thanks Chris for the explanation. Learned something very useful.

Similar Threads

  1. qbytearray qdatastream qfile writing/reading
    By pekal in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2012, 21:51
  2. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  3. QextSerialPort reading error: wrong values
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 6th May 2009, 19:29
  4. QDataStream
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2006, 14:40
  5. QDataStream
    By frankoz in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2006, 07:18

Tags for this Thread

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.