将C样式转换为Python样式整数

时间:2016-01-20 16:01:55

标签: python

我在VS 3.5中使用VS2015。我有一个由C程序提供的结构。结构有c_ulongs。我想将这些C样式的long转换为Python整数。

我的C数据如下所示:

class _C_vendrRecord(Structure):
    _pack_ = 1         # pack the struct
    _fields_ = [
    ("currentBalance",             c_ulong * (1)),
    ]
C_vendrRecord = _C_vendrRecord() # used to reference the C formatted data

我想说这样的话:

currentBalance = C_vendrRecord.currentBalance

我已经做了大量的搜索,但没有任何作用。我的尝试看起来像这样:

currentBalance = C_vendrRecord.currentBalance
print(currentBalance)
<vendr.c_ulong_Array_1 object at 0x016AD710>

currentBalance = C_vendrRecord.currentBalance.value
print(currentBalance)
AttributeError: 'c_ulong_Array_1' object has no attribute 'value'

而且,既然我看到了类似问题的帖子,我试过这个:

currentBalance = ctypes.c_long(1).value # in the post
print(currentBalance)
1

currentBalance = ctypes.c_long(C_vendrRecord.currentBalance).value
print(currentBalance)
TypeError was unhandled by user code
Message: an integer is required (got type c_ulong_Array_1)

1 个答案:

答案 0 :(得分:0)

问题是你已经指定了一个元素数组但是已经像访问标量那样访问它了。只需添加[0]即可,例如currentBalance = C_vendrRecord.currentBalance[0]它会起作用。