Check the in-memory bytes of a floating point number float in Python

Make a note because I always forget how to use ctyes. I want to see the in-memory bytes of a when a = 0.5.

>>> import ctypes
>>> a = ctypes.c_float(0.5)
>>> a
c_float(0.5)
>>> a.value
0.5
>>> ctypes.cast(ctypes.byref(a), ctypes.POINTER(ctypes.c_float))
<__main__.LP_c_float object at 0x000001FF2A2F59C8>
>>> ctypes.cast(ctypes.byref(a), ctypes.POINTER(ctypes.c_char))
<ctypes.LP_c_char object at 0x000001FF2A2F5B48>
>>> ctypes.cast(ctypes.byref(a), ctypes.POINTER(ctypes.c_char))[0:4]
b'\x00\x00\x00?'

If you do not specify the range with [0: 4], you will get angry. (Error if [:]) Corrected the output so that it is easy to understand.

>>> b = lambda f: [f'{b:02x}' for b in ctypes.cast(ctypes.byref(ctypes.c_float(f)), ctypes.POINTER(ctypes.c_char))[0:4]]
>>> b(0.5)
['00', '00', '00', '3f']

By the way, other numbers are

>>> b(0.25)
['00', '00', '80', '3e']
>>> b(0.866)
['2d', 'b2', '5d', '3f']
>>> b(0.433)
['2d', 'b2', 'dd', '3e']
>>> b(0.216)
['1b', '2f', '5d', '3e']
>>> b(0.0)
['00', '00', '00', '00']
>>> b(1.0)
['00', '00', '80', '3f']

Check the reverse (float from byte string).

>>> f = lambda b: ctypes.cast(ctypes.create_string_buffer(b), ctypes.POINTER(ctypes.c_float))[0]
>>> f(b'\x2d\xb2\x5d\x3f')
0.8659999966621399
>>> f(b'\x2d\xb2\xdd\x3e')
0.43299999833106995
>>> f(b'\x2d\xb2\x5d\x3e')
0.21649999916553497
>>> f(b'\x3a\xcd\x13\xbf')
-0.5773502588272095

Recommended Posts

Check the in-memory bytes of a floating point number float in Python
Check if the string is a number in python
Get the number of specific elements in a python list
Check the behavior of destructor in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Get the number of readers of a treatise on Mendeley in Python
Output the number of CPU cores in Python
Get the caller of a function in Python
Make a copy of the list in Python
Find the number of days in a month
Output in the form of a python array
I made a program to check the size of a file in Python
How to get the number of digits in Python
Get the size (number of elements) of UnionFind in Python
A reminder about the implementation of recommendations in Python
Check the asymptotic nature of the probability distribution in Python
How to identify the element with the smallest number of characters in a Python list?
[Python] A program that finds the shortest number of steps in a game that crosses clouds
How to check in Python if one of the elements of a list is in another list
[Python] Representing the number of complaints from life insurance companies in a bar graph
Check the processing time and the number of calls for each process in python (cProfile)
Find out the apparent width of a string in python
Check the operation of Python for .NET in each environment
Python --Find out number of groups in the regex expression
[Note] Import of a file in the parent directory in Python
[Homology] Count the number of holes in data with Python
Find the eigenvalues of a real symmetric matrix in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to determine the existence of a selenium element in Python
[Python] Let's reduce the number of elements in the result in set operations
Read the standard output of a subprocess line by line in Python
A function that measures the processing time of a method in python
Write the test in a python docstring
Display a list of alphabets in Python 3
Check if the URL exists in Python
Run the Python interpreter in a script
The result of installing python in Anaconda
Check the path of the Python imported module
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
The story of blackjack A processing (python)
Project Euler # 17 "Number of Characters" in Python
Get a capture of the entire web page in Selenium Python VBA
[Python] A program that calculates the number of chocolate segments that meet the conditions
If you want a singleton in python, think of the module as a singleton
[Python] A program that calculates the number of socks to be paired
Generate a list packed with the number of days in the current month.
[Python] How to put any number of standard inputs in a list
[Python] Calculate the number of digits required when filling in 0s [Note]
Test & Debug Tips: Create a file of the specified size in Python
Receive a list of the results of parallel processing in Python with starmap
Get a datetime instance at any time of the day in Python
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Count the number of times two values appear in a Python 3 iterator type element at the same time
4 methods to count the number of occurrences of integers in a certain interval (including imos method) [Python implementation]
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
Draw a graph of a quadratic function in Python
[python] Check the elements of the list all, any