Test Float Pack and Unpack
# ---------------------------------------------------------------
# ---- test/display struct float packing and unpacking
#
# Note: the Python struct modules 'f' and 'd' conversion codes
# uses the packed representation IEEE 754, binary32
# (for 'f') or binary64 (for 'd') format, regardless
# of the floating-point format used by the platform
# ---------------------------------------------------------------
import struct
from my_hex_dump import hex_dump
import user_interface as ui
# ---------------------------------------------------------------
# ---- create a string from a byte array
# ---------------------------------------------------------------
def my_byte_string(byts:bytes) -> None:
lst = []
for b in byts: lst.append('\\x' + f'{b:02X} ')
return ''.join(lst)
# ---------------------------------------------------------------
# ---- process user input
# ---------------------------------------------------------------
def test_struct_float_pack_unpack():
while True:
print()
s = ui.get_user_input('Enter a float: ')
if not s: break
tf,flt = ui.is_float(s)
if tf is not True:
print('OOPS! Try again!')
continue
print()
pflt = struct.pack('f', flt) # pack float
print(f'flt type = {type(flt)}')
print(f'pflt type = {type(pflt)}')
print(f'pflt len = {len(pflt)}')
##print(f'pflt = {pflt}')
print(f'pflt = {my_byte_string(pflt)}')
##hex_dump(pflt,0,len(pflt)-1)
print()
[xflt] = struct.unpack('f', pflt) # unpack float
print(f'xflt type = {type(xflt)}')
print(f'xflt = {xflt}')
sflt = str(xflt)
print(f'sflt type = {type(sflt)}')
print(f'sflt = {sflt}')
# ---------------------------------------------------------------
# ---- main
# ---------------------------------------------------------------
test_struct_float_pack_unpack()
System Float Data Type
# ---------------------------------------------------------------
# ---- system's float max, min, epsilon
# ---------------------------------------------------------------
import sys
print("Max float value:", sys.float_info.max)
print("Min float value:", sys.float_info.min)
print("Machine epsilon:", sys.float_info.epsilon)
Display Integer or Float as a String of Bits
#!/usr/bin/python3
# ===============================================================
# display integer or float as a string of bits
# '0' or '1' characters (all together or as separate bytes)
#
# For struct see: docs.python.org/3/library/struct.html
# ===============================================================
import struct
# ---------------------------------------------------------------
# ---- is a string an integer (function is not currently used)
# ---------------------------------------------------------------
def is_string_an_integer(s):
return s.isdigit()
# ---------------------------------------------------------------
# ---- convert a number (float or integer) to a string of bits
# ---------------------------------------------------------------
def binary(s:str,separate=True) ->str:
# ---- integer
tf,num = ui.is_integer(s)
if tf:
print()
print(f'convert "{s}" ({num}) to a string of bits')
if separate:
bits = ''.join(f'{byt:0>8b} ' \
for byt in struct.pack('i',num))
else:
bits = ''.join(f'{byt:0>8b}' \
for byt in struct.pack('i',num))
return bits
# ---- float (4 bytes)
tf,num = ui.is_float(s)
if tf:
print()
print(f'convert "{s}" ({num}) to a string of bits')
if separate:
bits = ''.join(f'{byt:0>8b} ' \
for byt in struct.pack('f',num))
else:
bits = ''.join(f'{byt:0>8b}' \
for byt in struct.pack('f',num))
return bits
# ---- not a number
return None
# ---------------------------------------------------------------
# ---- main
# ---------------------------------------------------------------
if __name__ == '__main__':
import sys
import user_interface as ui
print()
print(f'System is {sys.byteorder} endia')
print()
while True:
print()
s = ui.get_user_input('Enter a number: ')
if not s: break
bits = binary(s)
if bits is None:
print()
print(f'bad input ({s}) - try again')
continue
print()
print(bits)