6

使用Python 的 CFFI 库,我试图将 Python 字符串哄骗到 char* 中,以便我可以将它传递给接受 char* 的 C 函数。我似乎无法弄清楚正确的咒语是什么。

考虑这个例子:

>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.new("char[]", "bob")

结果是:

TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str

以下也不起作用:

>>> ffi.new("char*", "bob")

它说:

TypeError: initializer for ctype 'char' must be a bytes of length 1, not str
4

1 回答 1

12

o11c 已经解决了这个问题:

明确选择一个编码,例如"bob".encode('ascii'). 这在 Python 3 中似乎只是必要的。

于 2017-12-05T13:51:36.960 回答