Python Forum
platform binary representation --- 3 questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
platform binary representation --- 3 questions
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
You can use the struct library to pack various sized things to a bytearray.
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 926 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  Why int() cannot pass a string representation of a float into int? majorjohnusa 1 1,885 Jul-09-2020, 05:26 AM
Last Post: Knight18
  Questions re: my first python app (GUI, cross-platform, admin/root-level commands)? DonnyBahama 0 1,724 Feb-27-2020, 08:14 PM
Last Post: DonnyBahama
  File system representation in a data structure Alfalfa 1 2,058 Dec-18-2019, 01:56 AM
Last Post: Alfalfa
  hex file to binary or pcap to binary baran01 1 5,683 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Generate String Only Unique to That Platform firesh 2 2,092 Sep-10-2019, 09:36 PM
Last Post: firesh
  Python code for gcode reader and representation ralmeida 1 6,242 Jul-31-2018, 09:20 AM
Last Post: DeaD_EyE
  turtle moving platform chappie 1 3,010 Dec-06-2017, 10:52 PM
Last Post: chappie
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,510 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020