1.889...">
美文网首页
十六进制字符串转换为浮点数

十六进制字符串转换为浮点数

作者: 羋学僧 | 来源:发表于2020-07-29 10:14 被阅读0次

    如何在Python中将以下十六进制字符串转换为float(单精度32位)?

    "41973333" -> 1.88999996185302734375E1
    "41995C29" -> 1.91700000762939453125E1
    "470FC614" -> 3.6806078125E4

    方法一

    >>> import struct
    >>> struct.unpack('!f', '41973333'.decode('hex'))[0]
    18.899999618530273
    >>> struct.unpack('!f', '41995C29'.decode('hex'))[0]
    19.170000076293945
    >>> struct.unpack('!f', '470FC614'.decode('hex'))[0]
    36806.078125
    

    方法二

    from ctypes import *
    
    def convert(s):
        i = int(s, 16)                   # convert from hex to a Python int
        cp = pointer(c_int(i))           # make this into a c integer
        fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float pointer
        return fp.contents.value         # dereference the pointer, get the float
    
    print convert("41973333")    # returns 1.88999996185302734375E1
    
    print convert("41995C29")    # returns 1.91700000762939453125E1
    
    print convert("470FC614")    # returns 3.6806078125E4
    

    方法三

    import struct 
    
    testcases = {
    "41973333": 1.88999996185302734375E1,
    "41995C29": 1.91700000762939453125E1,
    "470FC614": 3.6806078125E4,
    }
    
    def hex2float(s):
        bins = ''.join(chr(int(s[x:x+2], 16)) for x in range(0, len(s), 2))
        return struct.unpack('>f', bins)[0]
    
    for s in testcases:
      print(hex2float(s), testcases[s])
    
    #18.8999996185 18.8999996185
    #19.1700000763 19.1700000763
    #36806.078125 36806.078125
    

    合集

    import struct
    import ctypes
    
    def f2h(s):
        fp = ctypes.pointer(ctypes.c_double(s))
        cp = ctypes.cast(fp, ctypes.POINTER(ctypes.c_longlong))
        return hex(cp.contents.value)
    
    def float_to_hex(f):
        return hex(struct.unpack('<I', struct.pack('<f', f))[0])
    
    def float2hex(s):
        fp = ctypes.pointer(ctypes.c_float(s))
        cp = ctypes.cast(fp,ctypes.POINTER(ctypes.c_long))
        return hex(cp.contents.value)
    
    def h2f(s):
        cp = ctypes.pointer(ctypes.c_longlong(s))
        fp = ctypes.cast(cp, ctypes.POINTER(ctypes.c_double))
        return fp.contents.value
    
    def hex_to_float(h):
        i = int(h,16)
        return struct.unpack('<f',struct.pack('<I', i))[0]
    
    def hex2float(h):
        i = int(h,16)
        cp = ctypes.pointer(ctypes.c_int(i))
        fp = ctypes.cast(cp,ctypes.POINTER(ctypes.c_float))
        return fp.contents.value
    
    if __name__ == '__main__':
        f = [1.5,-1.5,3.5,-3.5]
        h = []
        for i in f:
            print(f2h(i),"   |   ",float_to_hex(i),"   |   ",float2hex(i))
            h.append(float_to_hex(i))
        print(h)
        for i in h :
            print(h2f(i),"   |   ",hex_to_float(i),"   |   ",hex2float(i))
    

    学习来源

    相关文章

      网友评论

          本文标题:十六进制字符串转换为浮点数

          本文链接:https://www.haomeiwen.com/subject/ththrktx.html