# Convert floating point number from binary to a decimal number

I have to convert floating point number from binary to usable decimal number.

Of course my floating point number has been separated into bytes, so 4 bytes total.
1 2 3 4
[xxxxxxxx][xxxxxxxx][xxxxxxxx][xxxxxxxx]

These 4 bytes are already converted to decimal, so I have e.g.
1 2 3 4


Now Mantissa is held in three parts, two rightmost bytes (3 & 4) and in byte 2 but without the MSB bit (that one is easy to mask out, so let's assume we have a nice decimal number there as well). So three decimal numbers.

Is there an straightforward mathematical conversion to a floating point mantissa for these three decimal numbers?

This is along the lines: if I needed to get an integer, the formula would be
10 * 65536 + 104 * 256 + 79.

• Why are you doing it by hand? – Pieter Geerkens Mar 13 '13 at 18:09
• Is this an IEEE 754 floating point number, or some other format? – sfstewman Mar 13 '13 at 18:14
• If it is IEEE 754, the answer is no, it is not possible to give a simple formula to convert the mantissa, because its interpretation depends on the exponent. – Patricia Shanahan Mar 13 '13 at 19:03
• sfstewman: No, it is a single precision 32 bits floating point created in C on UNIX machine. Sign bit: 1 bit Exponent width: 8 bits Significant precision: 24 (23 explicitly stored) Patricia: I have the full bit set so even if it is dependent on the exponent, conversion is possible since the data is there. Pieter: Because I am doing it in a third party software with no methods available for such a task. – pjercic Mar 13 '13 at 19:09
• @pjercic: That's most likely a single-precision IEEE 754 floating point number, however you should really verify this, because the format is important, and neither C nor Unix, themselves, are clear enough to infer the format. Both C and Unix exist for a variety of architectures, and a variety of floating point formats. – sfstewman Mar 13 '13 at 19:14

Call these bytes `a`, `b`, and `c`. I assume `a` has already been masked, so it contains only the bits of the significand and none of the exponent, and that the number is IEEE-754 32-bit binary floating-point, with bytes taken with the appropriate endianness.

If the raw exponent field is 1 to 254 (thus, not 0 or 255), then the significand is:

``````1 + a*0x1p-7 + b*0x1p-15 + c*0x1p-23
``````

or, equivalently:

``````(65536*a + 256*b + c) * 0x1p-23 + 1.
``````

If the raw exponent field is 0, then remove the 1 from the sum (the number is subnormal or zero). If the raw exponent field is 255, then the floating-point value is infinity (if `a`, `b`, and `c` are all 0) or a NaN (otherwise).

• This answer presumes an IEEE 754 single-precision float. – sfstewman Mar 13 '13 at 19:15
• Sorry, just to ask 0x1p-23 is the scaling factor and it means 1 * 2 ^ -23 which is 0.00000011920928955078125? Am I right? – pjercic Mar 14 '13 at 13:43
• @pjercic: Yes. Hexadecimal floating-point numerals are part of C. They have the form “0x”, some hexadecimal digits optionally including a period, “p”, and a decimal exponent optionally including a sign. The exponent is a power of two. Hexadecimal numerals are a good way to express binary floating-point values precisely. – Eric Postpischil Mar 14 '13 at 13:53
• Sorry, I actually deleted the comment since the error was in using on the server side an ntohl() on a float number, which corrupts it heavily. Mark my words, ntohl() is an enemy of the float. Thank you for the answer, I got it working now. – pjercic Mar 14 '13 at 17:29

I cannot be of much help, since it has been a while since I did conversions, but I hope you find this tutorial useful.

• Thanks, I followed this one. – pjercic Mar 13 '13 at 19:58
{"ft_lang_label":"__label__en","ft_lang_prob":0.86985105,"math_prob":0.9552737,"size":740,"snap":"2020-34-2020-40","text_gpt3_token_len":192,"char_repetition_ratio":0.14809783,"word_repetition_ratio":0.0,"special_character_ratio":0.28513515,"punctuation_ratio":0.09210526,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99293303,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-08T03:11:25Z\",\"WARC-Record-ID\":\"<urn:uuid:d2294cf5-b4af-4b38-9a15-7f0ed176c979>\",\"Content-Length\":\"163862\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0c36f849-9f14-4724-9447-8b32383e227d>\",\"WARC-Concurrent-To\":\"<urn:uuid:69875353-51ed-4b70-a200-dc4b6e09ddd1>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/15393113/convert-floating-point-number-from-binary-to-a-decimal-number/\",\"WARC-Payload-Digest\":\"sha1:AOXMCWCT6TKCJGOPRVRUU5DL6SFK2PPD\",\"WARC-Block-Digest\":\"sha1:FAVBVAMJPGS233CDWGKZYP3NLNKGNELW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439737238.53_warc_CC-MAIN-20200808021257-20200808051257-00040.warc.gz\"}"}