读取32位浮点二进制数据文件(小端) [英] Reading a 32 bit floating point binary data file (little endian)

查看:240
本文介绍了读取32位浮点二进制数据文件(小端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个二进制数据文件,该文件包含python中的32位浮点二进制数据。我尝试在文件上使用hexdump,然后在python中读取hexdump。当转换回float时,某些值返回nan。我检查了合并十六进制转储值时是否出错,但找不到任何错误。这就是我在shell中所做的事情:

I want to read a binary data file that contains 32 bit floating point binary data in python. I tried using hexdump on the file and then reading the hexdump in python. Some of the values when converted back to float returned nan. I checked if I made a mistake in combining the hexdump values but couldn't find any. This is what I did this in shell:

hexdump -vc >> output.txt

hexdump -vc >> output.txt

输出为形式
c0 05 e5 3f ...等等

The output was of the form c0 05 e5 3f ... and so on

我加入了十六进制:'3fe505c0'

I joined the hex as : '3fe505c0'

这是执行此操作的正确方法吗?

Is this the correct way to do this ?

推荐答案

否。

>>> import struct
>>> struct.unpack('<f', '\xc0\x05\xe5\x3f')
(1.7892379760742188,)

这篇关于读取32位浮点二进制数据文件(小端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆