c - 如何在 python 中使用 struct 模块解压缩字节?

标签 c struct python-3.x

我正在尝试在 python 中解压字节:-

import struct


c_struct_exp='struct lokesh { int i=5;} lm;'
result=struct.unpack('!i',bytes(c_struct_exp,'utf-8'))  
print(result)

错误:

 struct.error: unpack requires a bytes object of length 4

请帮我在 unpack 方法中格式化字符串表达式。

最佳答案

unpack用于解压二进制数据,而不是 C 源代码。按照您的单个整数成员结构示例:

>>> from struct import *
>>> pack('i', 134)
'\x86\x00\x00\x00'
>>> unpack('i', '\x86\x00\x00\x00')
(134,)
>>>

关于c - 如何在 python 中使用 struct 模块解压缩字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14522535/

相关文章:

c - C中的二进制除法

c# - 将 C++ 结构数组编码为 C#

python - 无法从某些盒状容器中获取标题

python - 迭代并行列表并将一个列表中的制表符分隔文本转换为其他列表中的 csv 路径

c++ - unsigned char 的按位运算

c - 将整数插入通过 C 中的指针访问的数组中

Swift:如何将类更改为结构

python 3.X : How to insert new row in excel using Xlrd3 library

c - 登录 C 的问题

c++ - 使用命名空间时如何在结构本身中定义静态结构成员