在 Python 中进行位字段操作的最佳方法是什么?
我正在阅读一些基于 UDP 的 MPEG 传输流协议,其中有一些时髦的位字段(例如长度 13)。 我正在使用“struct”库进行广泛的解包,但是有没有一种简单的方法可以说“获取接下来的 13 位”,而不必手动调整位操作? 我想要类似 C 处理位字段的方式(无需恢复到 C)。
建议?
I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the way C does bit fields (without having to revert to C).
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bitstring 模块旨在解决这个问题。 它将让您使用位作为基本构建块来读取、修改和构造数据。 最新版本适用于 Python 2.6 或更高版本(包括 Python 3),但 1.0 版本也支持 Python 2.4 和 2.5。
一个与您相关的示例可能是这样的,它从传输流中去除所有空数据包(并且很可能使用您的 13 位字段?):
这是另一个示例,包括从比特流中读取:
您可以使用标准切片符号来切片、删除位级别的反转、覆盖等,还有位级别的查找、替换、分割等功能。 还支持不同的字节序。
完整文档位于此处。
The bitstring module is designed to address just this problem. It will let you read, modify and construct data using bits as the basic building blocks. The latest versions are for Python 2.6 or later (including Python 3) but version 1.0 supported Python 2.4 and 2.5 as well.
A relevant example for you might be this, which strips out all the null packets from a transport stream (and quite possibly uses your 13 bit field?):
Here's another example including reading from bitstreams:
You can use standard slice notation to slice, delete, reverse, overwrite, etc. at the bit level, and there are bit level find, replace, split etc. functions. Different endiannesses are also supported.
The full documentation is here.
这是一个经常被问到的问题。 上面有一个 ASPN Cookbook 条目,过去曾为我提供过帮助。
并且有一个人们希望从执行此操作的模块中看到大量的要求。
It's an often-asked question. There's an ASPN Cookbook entry on it that has served me in the past.
And there is an extensive page of requirements one person would like to see from a module doing this.