# -*- coding: utf8 -*-import ctypesdef h2f(s): cp = ctypes.pointer(ctypes.c_long(s)) fp = ctypes.cast(cp, ctypes.POINTER(ctypes.c_float)) return fp.contents.valuedef f2h(s): fp = ctypes.pointer(ctypes.c_float(s)) cp = ctypes.cast(fp, ctypes.POINTER(ctypes.c_long)) return hex (cp.contents.value)print (f2h( 3.1415 ))print (h2f( 0x40490e56 ))
#-------------------
ret = f2h(3.1415) # ret = "0x40490e56"ret2 = h2f(int(ret,16)) # ret2 = 3.14149999619
ret3 = "%.4f"%ret2 # ret3 = "3.1415"
ret4 = float(ret3) # ret4 = 3.1415版权声明:本文为zjl50原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。