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

convert uint8_t array to double

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.9k Views
  • 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 Offline
    G Offline
    GunkutA
    wrote on 20 Aug 2020, 08:30 last edited by
    #1

    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.

    J 1 Reply Last reply 20 Aug 2020, 08:49
    0
    • G GunkutA
      20 Aug 2020, 09:05

      @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.

      J Online
      J Online
      JonB
      wrote on 20 Aug 2020, 09:19 last edited by
      #4

      @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 20 Aug 2020, 09:25
      1
      • G GunkutA
        20 Aug 2020, 08:30

        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.

        J Online
        J Online
        JonB
        wrote on 20 Aug 2020, 08:49 last edited by
        #2

        @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 20 Aug 2020, 09:05
        1
        • J JonB
          20 Aug 2020, 08:49

          @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 Offline
          G Offline
          GunkutA
          wrote on 20 Aug 2020, 09:05 last edited by
          #3

          @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.

          J 1 Reply Last reply 20 Aug 2020, 09:19
          0
          • G GunkutA
            20 Aug 2020, 09:05

            @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.

            J Online
            J Online
            JonB
            wrote on 20 Aug 2020, 09:19 last edited by
            #4

            @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 20 Aug 2020, 09:25
            1
            • J JonB
              20 Aug 2020, 09:19

              @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 Offline
              G Offline
              GunkutA
              wrote on 20 Aug 2020, 09:25 last edited by
              #5

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

               convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
              
              J 1 Reply Last reply 20 Aug 2020, 09:47
              0
              • G GunkutA
                20 Aug 2020, 09:25

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

                 convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
                
                J Online
                J Online
                JonB
                wrote on 20 Aug 2020, 09:47 last edited by JonB
                #6

                @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
                0

                1/6

                20 Aug 2020, 08:30

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved