Congratulations to our 2022 Qt Champions!

QDataStream serialisation of double?

  • Consider the following snipet:

    @void myFunc()
    QFile *file = new QFile(fileName);
    if (!file->open(QFile::WriteOnly))
    QString err = file->errorString();
    QString *msgText = new QString("Could not open the file from disk!\n");
    QString *msgTitle = new QString("ERROR: Could not open the file!");
    emit errMsg(msgTitle, msgText, "WARNING");
    delete file;
    QDataStream out(file);
    double x = 2.0;
    out << x;

    Does this write 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 to the file?
    If yes, then how do I read the data from file bit-by-bit instead of the byte?
    Why I am asking this is because I have a binary file created on a PC using 16 bytes to store a double variable. And I want to read the file on a PC using 8 bytes to store a double.
    If I am able to read bit by bit then I can calculate the value to a double by converting the binary to decimal.

  • No, it does not!

    You are assuming an eight byte integer number, but you are writing a 8 byte floating point number. The data representation certainly different. "float number are divided into mantissa and exponent. ":
    You need to dig a bit to find the representation of 16 byte floats. On a PC-style architecture you have 10 byte floats (long double) as maximum to my understanding.

  • Could not see it right away, but in the same source there is also some "info on 16 byte floats. ":

  • Why do you feel that I am assuming an 8 byte int?
    The code writes double variable using QDataStream.
    The binary form for 2.0 for a double variable that I have mentioned is IEEE 754 standard. You can find the same "here":

  • OK, sorry answer was a bit premature and not completely thought through either. It looked at first glance like an integer representation. I have completely ignored the special cases for power of 2 :(

    You cannot read from a file bit by bit. Basically read a couple bytes (e.g. 8 or 16 for your 128 bit) representation and do bit manipulation on these bytes.
    I would assume that there is some source code around to do the manipulation, but I could find some right away.

Log in to reply