Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QDataStream writing wrong values
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QDataStream writing wrong values

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alok9871
    wrote on last edited by
    #1

    Hi,

    I'm building a program with a working thread.

    @
    QFile file("file.dat");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out.setByteOrder(QDataStream::LittleEndian);
    //out << QString("the answer is"); // serialize a string
    int x = 54;
    out << (qint32)x;
    out.device()->close();
    @

    I was expecting an output of 54. Itself it writing its char value, that is,6; same is tha case with other numbers.

    Please let me know how to address this problem

    Thanks

    Regards
    Alok

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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".

      @ const int num = 54;

      QFile out("out.txt");
      if (out.open(QIODevice::WriteOnly) {
      QTextStream s(&out);
      s << num;
      }
      

      @
      Also look at QString::number(), QByteArray::number() etc.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved