Solved convert uint8_t array to double
-
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. -
@GunkutA
Several possibilities. For a start, you should not assumeQByteArray 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 adouble
. You will have needed to receive at leastsizeof(PARAM_DATA_HEADER) + 8
bytes. Start by verifying you have at least that. -
@GunkutA
To be clear: adouble
will take up 8 bytes (uint8_t
for byte). Have you read in those 8 bytes atreceivedFrame.data + sizeof(PARAM_DATA_HEADER)
? (I assumereceivedFrame.data
is typeuint8_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? -
@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. -
@GunkutA
Several possibilities. For a start, you should not assumeQByteArray 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 adouble
. You will have needed to receive at leastsizeof(PARAM_DATA_HEADER) + 8
bytes. Start by verifying you have at least that. -
@JonB So does this casting look correct for converting uint8_t array to double?
convertedValueDouble = *(double*)(&receivedFrame.data[sizeof(PARAM_DATA_HEADER)]);
-
@GunkutA
Yes, assuming the bytes are all read, laid out in the right order at the right address, andreceivedFrame.data[]
is of typeu_int[]
. You shouldn't use your C-style cast these days, it should bereinterpret_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 usingg++
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 yoursizeof(PARAM_DATA_HEADER)
? If this is all not what you seem to assume, you could be pointing to rubbish data in memory....