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. QDataStream serialisation of double?
Qt 6.11 is out! See what's new in the release blog

QDataStream serialisation of double?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.6k 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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");
    msgText->append(err);
    QString *msgTitle = new QString("ERROR: Could not open the file!");
    emit errMsg(msgTitle, msgText, "WARNING");
    delete file;
    return;
    }
    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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. ":http://en.wikipedia.org/wiki/Double-precision_floating-point_format
      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        Could not see it right away, but in the same source there is also some "info on 16 byte floats. ":http://en.wikipedia.org/wiki/Long_double

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • CAD_codingC Offline
          CAD_codingC Offline
          CAD_coding
          wrote on last edited by
          #4

          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":http://www.binaryconvert.com/result_double.html?decimal=050.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            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