浮点数转换成十六进制(用python转换浮点数为16进制怎么写?比如把34.4536或者-34.4536转成16进制)
2022-09-10 01:25:53
摘要: 浮点数转换成十六进制(用python转换浮点数为16进制怎么写?比如把34.4536或者-34.4536转成16进制)...
# -*- coding: utf8 -*-import ctypesdef h2f(s): cp = ctypes.pointer(ctypes.c_longlong(s)
) fp = ctypes.cast(cp, ctypes.POINTER(ctypes.c_double)
) return fp.contents.valuedef f2h(s): fp = ctypes.pointer(ctypes.c_double(s)
) cp = ctypes.cast(fp, ctypes.POINTER(ctypes.c_longlong)
) return hex(cp.contents.value)print(f2h(34.4536)
)print(h2f(0x40413a0f9096bb99))