Python Forum
platform binary representation --- 3 questions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: platform binary representation --- 3 questions (/thread-31318.html)



platform binary representation --- 3 questions - Skaperen - Dec-04-2020

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?


RE: platform binary representation --- 3 questions - bowlofred - Dec-04-2020

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.


RE: platform binary representation --- 3 questions - deanhystad - Dec-04-2020

You can use the struct library to pack various sized things to a bytearray.


RE: platform binary representation --- 3 questions - DeaD_EyE - Dec-04-2020

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.


RE: platform binary representation --- 3 questions - Skaperen - Dec-05-2020

(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.