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.
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.
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)))[0]
# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack("Q", struct.pack("d",decVal))[0])
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)))[0]
# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack(">Q", struct.pack("d",decVal))[0])
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)))[0]
# Convert IEEE 754 64-bit Float to Hex
decVal=2.3456
print hex(struct.unpack("<Q", struct.pack("d",decVal))[0])
Checked via online converter:
http://babbage.cs.qc.cuny.edu/IEEE-754/