python 结构体解包
我正在尝试将以下 perl 代码: 转换
unpack(.., "Z*")
为 python,但是 struct.unpack() 中缺少“*”格式修饰符似乎使这变得不可能。有什么方法可以在 python 中做到这一点吗?
PS perldoc 中 perl 中的“*”修饰符 - 为重复计数提供 * 而不是数字意味着无论剩下多少项都可以使用,...
因此,尽管 python 具有像 perl 一样的数字重复计数,但它似乎缺乏a * 重复计数。
I'm trying to convert the following perl code:
unpack(.., "Z*")
to python, however the lack of a "*" format modifier in struct.unpack() seems to make this impossible. Is there a way I can do this in python?
P.S. The "*" modifier in perl from the perldoc - Supplying a * for the repeat count instead of a number means to use however many items are left, ...
So although python has a numeric repeat count like perl, it seems to lack a * repeat count.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
python 的
struct.unpack
没有Z
格式,我认为这
将是:
尽管这会去除空值
python's
struct.unpack
doesn't have theZ
formati think this
would be:
although that strips the nulls
我假设您创建了结构数据类型并且知道该结构的大小。如果是这种情况,那么您可以创建一个为该结构分配的缓冲区,并将值打包到缓冲区中。解包时,只需指定起点,即可使用相同的缓冲区直接解包。
例如
I am assuming that you create the struct datatype and you know the size of the struct. If that is the case, then you can create a buffer allocated for that struct and the pack the value into the buffer. While unpacking, you can use the same buffer to unpack directly by just specifying the starting point.
For e.g.
您必须自己计算重复计数:
我假设
your_fmt_string
不会解压多个元素,并且len(s)
完全除以该元素的打包大小。You must calculate the repeat count yourself:
I am assuming
your_fmt_string
doesn't unpack more than one element, andlen(s)
is perfectly divided by that element's packed size.