【问题标题】:How is the conversion in the python struct module done?python struct模块中的转换是如何完成的?
【发布时间】:2013-07-17 13:25:49
【问题描述】:

我需要从 C 结构中解压缩 python 中的信息, 通过以下代码进行操作:

struct.unpack_from('>I', file.read(4))[0]

然后,将更改的值写回:

new_value = struct.pack('>I', 008200)
file.write(new_value)

几个例子: 008200 返回语法错误:无效标记。 000010 写入:8 000017 写入:15 000017 返回语法错误。

我不知道那是什么类型的转换。 任何形式的帮助都会很棒。

【问题讨论】:

    标签: python c struct


    【解决方案1】:

    这是无效的python代码,与struct模块无关。在 python 中,以零开头的数字是八进制(以 8 为基数)。因此,python 尝试以八进制解码 008200,但 '8' 无效。假设您想要十进制,请使用 8200。如果您想要十六进制,请使用 0x8200。

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      相关资源
      最近更新 更多