Python 中的二进制 16
当您尝试使用 struct
模块时,该模块非常有用将数据与二进制格式相互转换。然而,最近我遇到了一个使用二进制16浮点格式的文件格式规范。我浏览了 Python 文档,但找不到任何可以与其相互转换的内容。将此数据与 Python 浮点数相互转换的最佳方法是什么?
The struct
module is useful when you're trying to convert data to and from binary formats. However, recently I came across a file format specification that uses the binary16 floating point format. I looked through the Python documentation, but can't find anything that can convert to and from it. What would be the best way to convert this data to/from Python floats?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以大致像用 C 语言那样做——也就是说,我认为,大致是这样的......:
You can do it roughly like you'd do it in C -- i.e., I think, roughly like this...:
这个人的博客文章给出了一个实现和蟒蛇。他使用
struct
模块,然后手动对其进行解码。转换并不那么复杂。This guy's blog post gives an implementation in both and python. He uses the
struct
module, then decodes it manually. It is not all that complicated a conversion.快速谷歌搜索出现了http://packages.python.org/bigfloat/,上面写着有一个用于操作
binary16
浮点数的上下文。不过,我自己对这个包并不熟悉,所以我无法告诉您有关如何使用它的任何信息(至少,您可以自己阅读文档)。A quick Google search turned up http://packages.python.org/bigfloat/ which says it has a context for manipulation of
binary16
floating-point numbers. I'm not familiar with the package myself, though, so I couldn't tell you anything about how to use it (at least, nothing more than you can read yourself in the documentation).