# Python program to represent floating number as hexadecimal by IEEE 754 standard

Prerequisite : IEEE Standard 754 Floating Point Numbers

Given a floating point number, the task is to find the hexadecimal representation for the number by IEEE 754 standard.

The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation which was established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and reduced their portability. IEEE Standard 754 floating point is the most common representation today for real numbers on computers, including Intel-based PC’s, Macs, and most Unix platforms.

Examples :

```Input :  -6744.90
Output :  C5D2C733

Input :  -263.3
Output :  C383A666
```

Approach :

• Check whether the number is positive or negative. Save the sign as 0 for positive and 1 for negative, and then convert the number into positive if it is negative.
• Convert the floating point number to binary.
• Separate the decimal part and the whole number part.
• Calculate the exponent(E) and convert it to binary.
• Find the mantissa.
• Concatinate the sign of mantissa, exponent and the mantissa.

Let’s write a Python program to represent a floating number as hexadecimal by IEEE 754 standard.

 `# Function for converting decimal to binary ` `def` `float_bin(number, places ``=` `3``): ` `    ``whole, dec ``=` `str``(number).split(``"."``)  ` `    ``whole ``=` `int``(whole)  ` `    ``dec ``=` `int` `(dec) ` `    ``res ``=` `bin``(whole).lstrip(``"0b"``) ``+` `"."` ` `  `    ``for` `x ``in` `range``(places):  ` `        ``whole, dec ``=` `str``((decimal_converter(dec)) ``*` `2``).split(``"."``)  ` `        ``dec ``=` `int``(dec)  ` `        ``res ``+``=` `whole    ` `    ``return` `res  ` ` `  `def` `decimal_converter(num):   ` `    ``while` `num > ``1``:  ` `        ``num ``/``=` `10` `    ``return` `num  ` ` `  `def` `IEEE754(n) : ` ` `  `    ``# identifying whether the number ` `    ``# is positive or negative ` `    ``sign ``=` `0` `    ``if` `n < ``0` `: ` `        ``sign ``=` `1` `        ``n ``=` `n ``*` `(``-``1``) ` `    ``p ``=` `30` ` `  `    ``# convert float to binary ` `    ``dec ``=` `float_bin (n, places ``=` `p) ` ` `  `    ``# separate the decimal part ` `    ``# and the whole number part ` `    ``whole, dec ``=` `str``(dec).split(``"."``) ` `    ``whole ``=` `int``(whole) ` ` `  `    ``# calculating the exponent(E) ` `    ``exponent ``=` `len``(``str``(whole)) ``-` `1` `    ``exponent_bits ``=` `127` `+` `exponent ` ` `  `    ``# converting the exponent from ` `    ``# decimal to binary ` `    ``exponent_bits ``=` `bin``(exponent_bits).lstrip(``"0b"``) ` ` `  `    ``# finding the mantissa ` `    ``mantissa ``=` `str``(whole)[``1``:exponent ``+` `1``] ` `    ``mantissa ``=` `mantissa ``+` `dec ` `    ``mantissa ``=` `mantissa[``0``:``23``] ` ` `  `    ``# the IEEE754 notation in binary ` `    ``final ``=` `str``(sign) ``+` `str``(exponent_bits) ``+` `mantissa ` ` `  `    ``# convert the binary to hexadecimal ` `    ``hstr ``=` `'%0*X'` `%``((``len``(final) ``+` `3``) ``/``/` `4``, ``int``(final, ``2``)) ` ` `  `    ``# return the answer to the driver code ` `    ``return` `(hstr) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `    ``print` `(IEEE754(``263.3``)) ` `    ``print` `(IEEE754(``-``263.3``)) `

Output:

```4383A666
C383A666
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.