Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved convert uint8_t array to double

    General and Desktop
    2
    6
    695
    Loading More Posts
    • 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.
    • G
      GunkutA last edited by

      Hello I am trying to convert received uint8_t array to double and then show it on the table. The value is 9.9 but the value on the table is 1.6976e-314. So I think I am having a problem during the convention. I am trying this way to convert it to double :

                convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
                 StringToDisplay.setNum(convertedValueDouble);
      

      So is there another way that I can use in order to achieve my goal?
      P.S = data is uint8_t pointer and dataLength is integer.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @GunkutA last edited by

        @GunkutA
        Several possibilities. For a start, you should not assume QByteArray receivedData= serial->readAll(); will read all the data you are expecting. That's not how it works. Check the actual length of data returned, receivedData.size().

        I note that your 0xcebb64 seems only to be 3 bytes size. 8 bytes are the representation of a double. You will have needed to receive at least sizeof(PARAM_DATA_HEADER) + 8 bytes. Start by verifying you have at least that.

        G 1 Reply Last reply Reply Quote 1
        • JonB
          JonB @GunkutA last edited by

          @GunkutA
          To be clear: a double will take up 8 bytes (uint8_t for byte). Have you read in those 8 bytes at receivedFrame.data + sizeof(PARAM_DATA_HEADER)? (I assume receivedFrame.data is type uint8_t *?) How about printing out the 8 bytes at that memory address to be sure we all know what number you are trying to convert?

          G 1 Reply Last reply Reply Quote 1
          • G
            GunkutA @JonB last edited by

            @JonB I have read all bytes in here:

                QByteArray receivedData= serial->readAll();
                uint8_t rawData[receivedData.size()];
                memcpy(rawData,receivedData.data(), receivedData.size());
                emit sendFrametoForm(rawData,receivedData.size());
            

            Then I send it to the function where I do :

             FRAME receivedFrame;
                
                memcpy(&receivedFrame,data,dataLength);
                        convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
                        StringToDisplay.setNum(convertedValueDouble);
            

            receivedFrame.data is uint8_t array.
            And when I printf the whole receivedFrame.data; the result is 0xcebb64. I don't know if it gives any clue.. And sorry about the bad explanation, the code is just a little bit messy.

            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @GunkutA last edited by

              @GunkutA
              Several possibilities. For a start, you should not assume QByteArray receivedData= serial->readAll(); will read all the data you are expecting. That's not how it works. Check the actual length of data returned, receivedData.size().

              I note that your 0xcebb64 seems only to be 3 bytes size. 8 bytes are the representation of a double. You will have needed to receive at least sizeof(PARAM_DATA_HEADER) + 8 bytes. Start by verifying you have at least that.

              G 1 Reply Last reply Reply Quote 1
              • G
                GunkutA @JonB last edited by

                @JonB So does this casting look correct for converting uint8_t array to double?

                 convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
                
                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @GunkutA last edited by JonB

                  @GunkutA
                  Yes, assuming the bytes are all read, laid out in the right order at the right address, and receivedFrame.data[] is of type u_int[]. You shouldn't use your C-style cast these days, it should be reinterpret_cast<> (I think, I have to look up the various ..._cast<> flavors to be sure), but that should not actually affect the result here.

                  [I didn't know you could do your uint8_t rawData[receivedData.size()]; declaration (variable for size). Are you using g++ to do this, it has an extension allowing this? Or is there some C++ level (C++ 14??) now which does allow this? But if it compiles I guess it must be OK.]

                  So what value is your receivedData.size()? What value is your sizeof(PARAM_DATA_HEADER)? If this is all not what you seem to assume, you could be pointing to rubbish data in memory....

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post