如何转换 Perl 的包“Nc*” Python 的 struct.pack 格式?

发布于 2024-08-06 19:31:09 字数 257 浏览 6 评论 0原文

我正在尝试将 Perl 脚本转换为 python,它使用了很多不同的包。我已经能够找出每个“模板”中的字母差异,但我在理解如何处理 Perl 缺乏长度声明方面遇到了问题。

示例:

pack('Nc*',$some_integer,$long_array_of_integers);

我在 Python 上的 struct.pack 中没有看到与此“*”功能类似的内容。关于如何将其转换为 Python 有什么想法吗?

I'm trying to convert a Perl script to python, and it uses quite a few different packs. I've been able to figure out the lettering differences in the "templates" for each one, but I'm having an issue with understanding how to handle Perl's lack of length declaration.

example:

pack('Nc*',$some_integer,$long_array_of_integers);

I don't see an analog for this "*" feature in struct.pack, on Python. Any ideas on how to convert this to Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-08-13 19:31:09

这个怎么样?

struct.pack('>I', some_integer) + struct.pack('b'*len(long_array), *long_array)

How about this?

struct.pack('>I', some_integer) + struct.pack('b'*len(long_array), *long_array)
舂唻埖巳落 2024-08-13 19:31:09

Perl 的包使用类似于正则表达式中的“*”字符——表示更多相同内容的通配符。当然,这里意味着更多的有符号整数。

在 Python 中,您只需循环遍历字符串并连接各个部分:

result = struct.pack('>L', some_integer)
for c in long_array_of_integers:
    result += struct.pack('b',c)

Perl's pack is using the '*' character similar to in regular expressions--meaning a wildcard for more of the same. Here, of course, it means more signed ints.

In Python, you'd just loop through the string and concat the pieces:

result = struct.pack('>L', some_integer)
for c in long_array_of_integers:
    result += struct.pack('b',c)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文