我正在使用 cffi,但遇到了一个奇怪的错误。我的函数接受一个字节数组并对其进行解密并从中创建一个人类可读的字符串。
buffer = [b'\x02', b'+', .... and more]
c_string = ffi.new("char[]", 64)
clib.decode_my_string(buffer, len(buffer), c_string, len(c_string))
print(c_string)
期望看到的是一个英文字符串。蟒蛇说:
TypeError: an integer is required
以下是这些事物的定义方式:
print(clib.decode_my_string)
print(c_string)
print(buffer)
print(len(c_string))
print(len(buffer))
显示:
<cdata 'uint8_t(*)(uint8_t *, uint8_t, char *, uint16_t)' 0x7f6965aa7a50>
<cdata 'char[]' owning 64 bytes>
[b'\x02', b'+', .... and more]
64
13
从打印中,我们可以看到该函数涉及两个整数。一个 8 位整数和一个 16 位整数。我对这些参数做错了吗?
完整追溯:
Traceback (most recent call last):
File "/home/me/myproject/utilitieslib/tests/test_mymodule.py", line 187, in test_decode_my_string
x = mymodule.decode_my_string(response)
File "/home/me/myproject/utilitieslib/mymodule.py", line 242, in decode_my_string
clib.decode_my_string(buffer, len(buffer), c_string, len(c_string))
TypeError: an integer is required