0

EDIT: Solved this by searching get int bits from float then used the hex() method around the int value. Works perfectly.

import struct

def floatToBits(f):
s = struct.pack('>f', f)
return struct.unpack('>l', s)[0]

print(hex(floatToBits(501.0)))

I think someone already posted something like that below as a comment, so thanks!

user3530525
  • 691
  • 3
  • 8
  • 20
  • Have you tried searching for *"python ieee 754"* on a random search engine? :) Hint: [a similar question](http://stackoverflow.com/questions/6286033/reading-32-bit-signed-ieee-754-floating-points-from-a-binary-file-with-python) has already an answer, just use `pack` instead of `unpack`. – Andrea Corbellini May 04 '14 at 20:27
  • 1
    Also, Python `float`s have had a [`.hex()` method](https://docs.python.org/2/library/stdtypes.html#float.hex) since 2.6. – jonrsharpe May 04 '14 at 20:28
  • Use struct.pack: `struct.pack('>f',500.0)` -> `'C\xfa\x00\x00'` – Daniel May 04 '14 at 20:29
  • Do all your floats end in `.0`, or do you need to be able to deal with the decimal part as well? If it's okay to ignore it, `hex(int())` returns a hexadecimal string. – Matthew May 04 '14 at 20:29

0 Answers0