python 4字的16进制数如何转换成浮点数?

拙者 2015-06-29 01:12:50
在python中像这样 四个字的0x0 0xff 0x7f 0x47 数如何转换成浮点数呢?
...全文
2063 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nore 2019-07-18
  • 打赏
  • 举报
回复

import struct
# python2
struct.unpack('!f', '41B3F048'.decode('hex'))[0]
# python3
struct.unpack('!f', bytes.fromhex('41B3F048'))[0]
这样十六进制很快就可以转换出来浮点数了
qq_33543369 2016-06-01
  • 打赏
  • 举报
回复
f = (0x0,0xff, 0x7f,0x47) for i in f: print float(i),
wlczhh 2016-03-22
  • 打赏
  • 举报
回复
怎么成s='\x00\xff\x7f\x47'
sprawling 2015-07-01
  • 打赏
  • 举报
回复
引用 2 楼 whw1874 的回复:
这样不行呀,会报错 。 >>> import struct >>> s = '0x0\0xff\0x7f\0x47' >>> print struct.unpack('!f',s)[0] Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> print struct.unpack('!f',s)[0] error: unpack requires a string argument of length 4 >>>
s='\x00\xff\x7f\x47'
楼上其实没错,就表达式没写好而已,其实是这样的。
拙者 2015-06-30
  • 打赏
  • 举报
回复
这样不行呀,会报错 。 >>> import struct >>> s = '0x0\0xff\0x7f\0x47' >>> print struct.unpack('!f',s)[0] Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> print struct.unpack('!f',s)[0] error: unpack requires a string argument of length 4 >>>
jlfzhz 2015-06-29
  • 打赏
  • 举报
回复
用struct模块(注意字节序):
import struct
s='0x0\0xff\0x7f\0x47'
print struct.unpack('!f',s)[0]
。。。。

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧