python struct模块
输入:4字节或8字节hex 字符串输出对应float或double数值
import struct
import binascii
u8_lit = struct.Struct('B')
s8_lit = struct.Struct('b')
u16_lit = struct.Struct('<H')
s16_lit = struct.Struct('<h')
u32_lit = struct.Struct('<I')
s32_lit = struct.Struct('<i')
double_lit = struct.Struct('<d')
float_lit = struct.Struct('<f')
u16_big = struct.Struct('>H')
s16_big = struct.Struct('>h')
u32_big = struct.Struct('>I')
s32_big = struct.Struct('>i')
u64_big = struct.Struct('>Q')
s64_big = struct.Struct('>q')
double_big = struct.Struct('>d')
float_big = struct.Struct('>f')
if __name__ == '__main__':
while True:
hex = input('输入hex:')
try:
test = hex.replace(' ', '')
print(test)
a = bytearray(binascii.a2b_hex(test))
if len(a) == 4:
c = float_big.unpack_from(a, 0)[0]
print(c)
elif len(a) == 8:
c = double_big.unpack_from(a, 0)[0]
print(c)
pass
except Exception as e:
print(e)