سؤال

I've got incoming data in Big Endian format

uint8_t u8DataA[] = {0x40, 0xAD, 0x70, 0xB8};

which needs to get unpacked to float. The result should be 3768.36. No mater how I stuff bytes into float I don't get this result. How do I do this?

هل كانت مفيدة؟

المحلول

You got the first four bytes of a 64-bit double representation, not the four bytes of a 32-byte representation.

According to the IEEE 754 calculator, 64-bit representation of 3768.36 is

0x40, 0xAD, 0x70, 0xB8, 0x51, 0xEB, 0x85, 0x1F
^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^
This is what you have   This part is missing

You took the first four bytes of it, and tried re-interpreting it as a float. That's why it did not work.

A 32-bit representation of 3768.36 is

0x45, 0x6B, 0x85, 0xC3

Converting it to float by stuffing the bytes into an array and re-interpreting produces the desired result (demo).

نصائح أخرى

Passing these hexadecimal number to decimal directly would be useless. If you know how to convert a decimal number to float, just try to convert these hex numbers to binary, and once you have an entire binary number (for example: 0000 1111 0010 1101 1111 1110 0001 0010. Having it as a string would help, but I don't know which functions nor how many functions allow you to work with binary numbers) you would only have to extract the exponent, sign and mantissa. If you don't know how to convert integers to float, have a look through the internet and choose the best tutorial for you. Keep in mind that, once you have the binary number as a string, taking the sign, mantissa and exponent is not much difficult. If you have some kind of function which converts binary to decimal, you can deal with that easily.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top