 # IEEE 754 standard - converting 64 bit hex to decimal

Did anyone attempt to convert ieee 754 (64 bit double-precision floating-point ) hex to float using python in Ignition. What are the ways of doing it?

Thanks.

1 Like

Give this a go:

``````import struct

# Convert IEEE 754 64-bit Hex to Float
hexString="4002c3c9eecbfb16"
print struct.unpack("d", struct.pack("Q",int("0x"+hexString, 16)))

# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack("Q", struct.pack("d",decVal)))
`````` Does it do it from right to left or left to right?

EDIT: Cleaned up post after migration. Left-to-right (big-endian, which is my system default). If you need to specify endianness:

big-endian:

``````import struct

# Convert IEEE 754 64-bit Hex to Float
hexString="4002c3c9eecbfb16"
print struct.unpack(">d", struct.pack("Q",int("0x"+hexString, 16)))

# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack(">Q", struct.pack("d",decVal)))
``````

little-endian:

``````import struct

# Convert IEEE 754 64-bit Hex to Float
hexString="4002c3c9eecbfb16"
print struct.unpack("<d", struct.pack("Q",int("0x"+hexString, 16)))

# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack("<Q", struct.pack("d",decVal)))
``````

Checked via online converter:
http://babbage.cs.qc.cuny.edu/IEEE-754/

1 Like