see also How to convert a float into hex for double to hex
import struct
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
doubleAsHex = double_to_hex(2.1)
print("doubleAsHex (correct):", doubleAsHex)
doubleAsBytes = bytearray.fromhex(doubleAsHex.replace('0x',''))
print("doubleAsBytes (missing first byte):", doubleAsBytes)
output:
doubleAsHex (correct): 0x4000cccccccccccd
doubleAsBytes (missing first byte): bytearray(b'@\x00\xcc\xcc\xcc\xcc\xcc\xcd')
Whats the reason for the omitted byte?