In [1]:
import struct
import binascii
In [2]:
f_max_s = '7fefffffffffffff'
In [3]:
print(binascii.unhexlify(f_max_s))
In [4]:
print(type(binascii.unhexlify(f_max_s)))
In [5]:
print(struct.unpack('>d', binascii.unhexlify(f_max_s)))
In [6]:
print(struct.unpack('>d', binascii.unhexlify(f_max_s))[0])
In [7]:
print(type(struct.unpack('>d', binascii.unhexlify(f_max_s))[0]))
In [8]:
def hex_to_double(s):
if s.startswith('0x'):
s = s[2:]
s = s.replace(' ', '')
return struct.unpack('>d', binascii.unhexlify(s))[0]
In [9]:
print(hex_to_double('7fefffffffffffff'))
In [10]:
print(hex_to_double('0x7fefffffffffffff'))
In [11]:
print(hex_to_double('0x7fef ffff ffff ffff'))
In [12]:
print(hex_to_double('0x4045 18f5 c28f 5c29'))
In [13]:
print(hex_to_double('7ff0000000000000'))
In [14]:
print(hex_to_double('7ff0000000000001'))
In [15]:
print(hex_to_double('0000000000000001'))
In [16]:
# print(hex_to_double('ffff ffff ffff ffff ff'))
# error: unpack requires a buffer of 8 bytes
In [17]:
# print(hex_to_double('ffff ffff ffff ff'))
# error: unpack requires a buffer of 8 bytes
In [18]:
def hex_to_float(s):
if s.startswith('0x'):
s = s[2:]
s = s.replace(' ', '')
return struct.unpack('>f', binascii.unhexlify(s))[0]
In [19]:
print(hex_to_float('0x4228c7ae'))