Python中hex与float互转

Python中hex与float互转

使用Python3内置方法struct

hex转float

import struct
a=[0x45,0xaf,0xb9,0xdd]
s=struct.unpack(">f",bytes(a))[0]  #5623.23291015625

b=`45afb9dd`
s=struct.unpack('>f',bytes.fromhex(b))[0]  # 5623.23291015625
  • 其中struct.unpack返回的是一个元组

float转hex

import struct
s=5623.23291015625
struct.pack('>f' ,float(s)).hex()  #`45afb9dd`

格式化字符

struct和格式化字符

猜你喜欢

转载自blog.csdn.net/wq_0708/article/details/121102261
今日推荐