Tobias
Tobias

Reputation: 947

Python generate IEEE 754 double-precision binary floating-point format

I need the IEEE 754 double-precision representation of a number for a compiler project I am currently working on. I was wondering if there is a module to generate the representation or if I need to implement it myself?

Upvotes: 2

Views: 1129

Answers (1)

Jon Clements
Jon Clements

Reputation: 142166

You can use struct.pack to generate the bytes then use the bytes.hex method to get it as a hex string, eg:

import struct

# >d = big-endian byte order double
rep = struct.pack('>d', 1.234).hex()
# 3ff3be76c8b43958

Upvotes: 3

Related Questions