0

Is there any code that allows me to convert 32-bit number in IEEE 754-floating point format back to decimal number? Preferably in C?


Sorry for my bad description.

Say I have 0x43ab9567 (this is in ieee754-floating point format), and I to convert to decimal.

0x43ab9567 (ieee754-floating point ) ->343.1672 (decimal)

andy_tse
  • 189
  • 1
  • 5
  • 13
  • Never heard of that format - where is it described? – 500 - Internal Server Error Jan 31 '15 at 23:06
  • Sorry, my bad. It is ieee 754 – andy_tse Jan 31 '15 at 23:09
  • Huh? what do you mean 'normal decimal'? Do you mean from a binary/hex string of an IEE-754 number to a decimal string? – aruisdante Jan 31 '15 at 23:15
  • You can store it in a 4-byte char array and then cast it to a `float *`. – Jongware Jan 31 '15 at 23:27
  • This post is unclear. How do you have `0x43ab9567`? Is that a string, text, an array of bytes, etc? Instead of discussing the number, post code that shows how you have `0x43ab9567`. – chux - Reinstate Monica Feb 01 '15 at 01:54
  • 2
    Your updated content is not anymore clearer. Do you want a string representation stored in `char[]`? In any case there are a lot of duplicates on SO: http://stackoverflow.com/questions/23191203/convert-float-to-string-without-sprintf http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i http://stackoverflow.com/questions/2268725/how-can-i-convert-a-float-double-to-ascii-without-using-sprintf-or-ftoa-in-c – phuclv Feb 01 '15 at 03:51

2 Answers2

3

Its is unclear whether you're looking for a string representation, mathematical transformation, or a tool to extract the components of a float:

To get a string representation, coerce to a double and call ftoa()?

float f = 3.14159;

printf("%s\n", ftoa((double) f)));

To extract the parts of the representation, coerce to a double, and then use frexp().

To manipulate in mathematically, using bit-shifts and and-masks to extract the three components (sign, exponent offset by 127, and the mantissa):

raw = 0x43ab9567;                
sign = raw >> 31;                            // 0
mantissa = (raw & 0x7FFFFF) | 0x800000;      // 11244903
exp = ((raw >> 23) & 0xFF) - 127 - 23;       // -15
result = mantissa * pow(2.0, exp);           // 343.1672
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
1

Please try this code:

union { uint32_t b; float f; } u;
u.b = 0x43ab9567;
printf("%g\n", u.f);
hichris123
  • 10,145
  • 15
  • 56
  • 70
AntoineL
  • 888
  • 4
  • 25
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Tieson T. Mar 05 '15 at 23:48
  • @TiesonT. sorry for the bad manners; I saw my post was edited, I hope to better reflect the expected style. However, I really think it provides an answer: the union is the mechanism to change between representations, and according to C standard §F.3, printf and the related functions "provide IEC 60559 binary-decimal conversions." – AntoineL Mar 07 '15 at 12:34
  • Yes, this method also does the math. – Mohammad Kanan Dec 19 '18 at 17:33