Python Forum

Full Version: platform binary representation --- 3 questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
for a given float value i want to get the platform binary representation as a sequence of bytes or a string of hexadecimal digits. is there a trivial way to do this? can it also work for ints up to the maximum value the platform can handle? how would i find what that maximum value is?
I'd start with the C api and hope it works for most cases. I don't know a pure python way to see it.

I assume you're talking cpython. This RealPython page takes you through how some of the integer and floats are handled as python objects.
You can use the struct library to pack various sized things to a bytearray.
In [11]: sys.float_info.max.hex()
Out[11]: '0x1.fffffffffffffp+1023'
I don't know since when it was introduced, but the hex method seems new to me.
I use Python 3.9, older version may not have it.
(Dec-04-2020, 07:24 AM)DeaD_EyE Wrote: [ -> ]
In [11]: sys.float_info.max.hex()
Out[11]: '0x1.fffffffffffffp+1023'
I don't know since when it was introduced, but the hex method seems new to me.
I use Python 3.9, older version may not have it.

i use 3.6 so i had to import sys first. then it worked.