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.6k 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.
  • 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 Offline
      Christian EhrlicherC Offline
      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 Offline
            Christian EhrlicherC Offline
            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