注意这里转换的是带有符号位的十六进制,无符号的直接俄 hex(十进制)就可以了
#coding=utf-8
import platform
import struct
python_version = platform.python_version()[0:1]
def float_to_16(float_num):
hex_string = struct.pack('h', float_num)
if python_version == "2":
"""python版本不同处理流程不同"""
print("2222")
hex_string2 = hex_string.encode('hex')
print (hex_string2)
elif python_version == "3":
print("3333")
hex_string2 = hex_string.hex()
print (hex_string2)
float_to_16(32)
python2输出:
E:condaaenvscv3python.exe E:/cv_two/Rebort2/cs1.py 2222 2000
python3输出:
PyDev console: starting.
Python 3.7.10 (default, Feb 26 2021, 13:06:18) [MSC v.1916 64 bit (AMD64)] on win32
runfile('E:/cv_one/Python/Rebort/cs1.py', wdir='E:/cv_one/Python/Rebort')
3333
2000



