[SOLVED]QDataStream writing wrong values



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.