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. Convert double to qbytearray
Forum Updated to NodeBB v4.3 + New Features

Convert double to qbytearray

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.4k Views 2 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.
  • D Offline
    D Offline
    Daniel Williams
    wrote on last edited by
    #1

    I have a variable defined as a double which I want to write that as binary to a blob field in sqlite. The sqlite api expects a QByteArray of the data to write to the field.

    double xValue = 70.976;
    QByteArray blobData;

    /*
    hex value of xValue is: 0x428df3b6
    The resulting blob should look something like:

    blobData[0] is 0x42 (01000010)
    blobData[1] is 0x8d (10001101)
    blobData[2] is 0xf3 (11110011)
    blobData[3] is 0xb6 (10110110)

    */

    How do I copy the xValue onto the end of the blobData QByteArray in binary form?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QByteArray::number comes to mind.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        A double value is not only 4 bytes long...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          A double value is not only 4 bytes long...

          D Offline
          D Offline
          Daniel Williams
          wrote on last edited by
          #4

          @Christian-Ehrlicher True that. But, they would have 0's for the other 4 bytes.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            QByteArray::number comes to mind.

            D Offline
            D Offline
            Daniel Williams
            wrote on last edited by
            #5

            @SGaist That's what I started with:

            blobData.append(QByteArray::number(xValue));

            What that does is write text to the field like a text field "70.976." That's not quite what I was hoping for.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @Daniel-Williams said in Convert double to qbytearray:

              True that. But, they would have 0's for the other 4 bytes.

              No, you're wrong. The byte representation you're showing in your first post is the float value of 70.976.

              float f = 70.976;
              const unsigned char *ptr = (const unsigned char*)(&f);
              for (int i = 0; i < 4; ++i)
                  fprintf(stderr, "%02x\n", ptr[i]);
              fprintf(stderr, "\n");
              double d = 70.976;
              const unsigned char *ptr2 = (const unsigned char*)(&d);
              for (int i = 0; i < 8; ++i)
                  fprintf(stderr, "%02x\n", ptr2[i]);
              

              -->

              b6
              f3
              8d
              42
              
              58
              39
              b4
              c8
              76
              be
              51
              40
              

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              D 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                @Daniel-Williams said in Convert double to qbytearray:

                True that. But, they would have 0's for the other 4 bytes.

                No, you're wrong. The byte representation you're showing in your first post is the float value of 70.976.

                float f = 70.976;
                const unsigned char *ptr = (const unsigned char*)(&f);
                for (int i = 0; i < 4; ++i)
                    fprintf(stderr, "%02x\n", ptr[i]);
                fprintf(stderr, "\n");
                double d = 70.976;
                const unsigned char *ptr2 = (const unsigned char*)(&d);
                for (int i = 0; i < 8; ++i)
                    fprintf(stderr, "%02x\n", ptr2[i]);
                

                -->

                b6
                f3
                8d
                42
                
                58
                39
                b4
                c8
                76
                be
                51
                40
                
                D Offline
                D Offline
                Daniel Williams
                wrote on last edited by
                #7

                @Christian-Ehrlicher Oh, I see what you're saying. And, in the process you showed me a way I may be able to do what I want. Once I have a const usigned char* to the xValue, I should be able to add 8 bytes to the blobData and then use memcpy to copy the bytes directly.

                D 1 Reply Last reply
                0
                • D Daniel Williams

                  @Christian-Ehrlicher Oh, I see what you're saying. And, in the process you showed me a way I may be able to do what I want. Once I have a const usigned char* to the xValue, I should be able to add 8 bytes to the blobData and then use memcpy to copy the bytes directly.

                  D Offline
                  D Offline
                  Daniel Williams
                  wrote on last edited by
                  #8

                  I'm working from home right now and can't get to the office to turn on the laser to test it fully, but this is what I'm thinking:

                  const unsigned char* dPtr = nullptr;

                  dataSize = blobData.length();
                  blobData.append(16, ' ');
                  dPtr = reinterpret_cast<const unsigned char*>(&xValue);
                  memcpy(outData.data()+dataSize, dPtr, 8);

                  Thank you again for the help.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Out of curiosity, why are you storing a double value as a blob in your database ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Out of curiosity, why are you storing a double value as a blob in your database ?

                      D Offline
                      D Offline
                      Daniel Williams
                      wrote on last edited by Daniel Williams
                      #10

                      @SGaist I get about 1040 x and a z values from the sdk that pulls the data from the laser device. Each tuple represents a point. x is the position on the laser line and z is the height at that point. I store each tuple consecutively in the blob. Then, the program that renders the image pulls the data from the blob and does the processing and display of the image.

                      Each 1040 points represents 1 line. I get thousands of lines in a matter of seconds. Each line is a row in the database. Each row in the database thus represents the y axis of a 3-dimensiaonal image.

                      1 Reply Last reply
                      0
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on last edited by
                        #11

                        Isnt this what QDataStream is for?

                        C++ is a perfectly valid school of magic.

                        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