无论你给它分配什么值,Python 结构总是停留在 0? [英] Python structure always stuck at 0 no matter what value you assign to it?

查看:34
本文介绍了无论你给它分配什么值,Python 结构总是停留在 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模块来压缩要传递给 C 程序的位,但不断出现错误.经过一些测试,我发现 Blah 类的字段 a 无论如何都卡在 0 上.有谁知道这是一个错误还是我在这里做错了什么?

抱歉,我忘了提到我正在使用来自 http: 的 python 3.1.2://www.python.org/download/releases/3.1.2/

<预><代码>>>>导入 ctypes>>>类废话(ctypes.Structure):... _fields_ = [("a", ctypes.c_uint64, 64),... ("b", ctypes.c_uint16, 16),... ("c", ctypes.c_uint8, 8),... ("d", ctypes.c_uint8, 8)]...>>>x = 废话(0xDEAD,0xBEEF,0x44,0x12)>>>打印(十六进制(x.a))0x0>>>打印(十六进制(x.b))0x牛肉>>>打印(十六进制(x.c))0x44>>>打印(十六进制(x.d))0x12>>>>>>g = 废话(0x2BAD,0xBEEF,0x55​​,0x12)>>>打印(十六进制(g.a))0x0>>>打印(十六进制(g.b))0x牛肉>>>打印(十六进制(g.c))0x55>>>打印(十六进制(g.d))0x12>>>

交换前两个字段的位置给出相同的结果

<预><代码>>>>导入 ctypes>>>类废话(ctypes.Structure):... _fields_ = [("a", ctypes.c_uint16, 16),... ("b", ctypes.c_uint64, 64),... ("c", ctypes.c_uint8, 8),... ("d", ctypes.c_uint8, 8)]...>>>x = 废话(0xDEAD,0xBEEF,0x44,0x12)>>>打印(十六进制(x.a))0x死>>>打印(十六进制(x.b))0x0>>>打印(十六进制(x.c))0x44>>>打印(十六进制(x.d))0x12>>>>>>g = 废话(0x2BAD,0xBEEF,0x55​​,0x12)>>>打印(十六进制(g.a))0x2坏>>>打印(十六进制(g.b))0x0>>>打印(十六进制(g.c))0x55>>>打印(十六进制(g.d))0x12>>>

改变字段的大小,我观察到输入的一些奇怪的截止

<预><代码>>>>导入 ctypes>>>类废话(ctypes.Structure):... _fields_ = [("a", ctypes.c_uint64, 40),... ("b", ctypes.c_uint64, 40),... ("c", ctypes.c_uint8, 8),... ("d", ctypes.c_uint8, 8)]...>>>x = 废话(0xDEAD,0xBEEF,0x44,0x12)>>>打印(十六进制(x.a))0xad>>>打印(十六进制(x.b))0xef>>>打印(十六进制(x.c))0x44>>>打印(十六进制(x.d))0x12>>>>>>g = 废话(0x2BAD,0xBEEF,0x55​​,0x12)>>>打印(十六进制(g.a))0xad>>>打印(十六进制(g.b))0xef>>>打印(十六进制(g.c))0x55>>>打印(十六进制(g.d))0x12>>>

有人知道为什么会这样吗?

解决方案

您可以省略第三个字段作为解决方法.

<预><代码>>>>导入 ctypes>>>类废话(ctypes.Structure):... _fields_ = [("a", ctypes.c_uint64), ('b', ctypes.c_uint16), ('c', ctypes.c_uint8), ('d', ctypes.c_uint8)]...>>>x = 废话(0xDEAD,0xBEEF,0x44,0x12)>>>十六进制(x.a)'0x死'>>>十六进制(x.b)'0x牛肉'

我想剩下的都是一个错误.

I was writing a module to compact bits to be passed to C program, but keep getting errors. After some tests, I found out that the field a of class Blah is stuck at 0 no matter what. Does anyone know if this is a bug or if I'm doing something wrong here?

Sorry, I forgot to mention I'm using python 3.1.2 from http://www.python.org/download/releases/3.1.2/

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint64, 64),
...                 ("b", ctypes.c_uint16, 16),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0x0
>>> print (hex(x.b ))
0xbeef
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0x0
>>> print (hex(g.b ))
0xbeef
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

swapping first two fields' position gives same result

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint16, 16),
...                 ("b", ctypes.c_uint64, 64),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0xdead
>>> print (hex(x.b ))
0x0
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0x2bad
>>> print (hex(g.b ))
0x0
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

varying field's size and I observe some weird cutoff of the input

>>> import ctypes
>>> class Blah(ctypes.Structure):
...     _fields_ = [("a", ctypes.c_uint64, 40),
...                 ("b", ctypes.c_uint64, 40),
...                 ("c", ctypes.c_uint8, 8),
...                 ("d", ctypes.c_uint8, 8)]
...
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> print (hex(x.a) )
0xad
>>> print (hex(x.b ))
0xef
>>> print (hex(x.c ))
0x44
>>> print (hex(x.d ))
0x12
>>>
>>> g = Blah(0x2BAD,0xBEEF,0x55,0x12)
>>> print (hex(g.a ))
0xad
>>> print (hex(g.b ))
0xef
>>> print (hex(g.c ))
0x55
>>> print (hex(g.d ))
0x12
>>>

Does anyone know why is this happening?

解决方案

You could omit the 3rd field as a workaround.

>>> import ctypes
>>> class Blah(ctypes.Structure):
...   _fields_ = [("a", ctypes.c_uint64), ('b', ctypes.c_uint16), ('c', ctypes.c_uint8), ('d', ctypes.c_uint8)]
... 
>>> x = Blah(0xDEAD,0xBEEF,0x44,0x12)
>>> hex(x.a)
'0xdead'
>>> hex(x.b)
'0xbeef'

I guess the rest is a bug.

这篇关于无论你给它分配什么值,Python 结构总是停留在 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆