使用 CFFI library for Python ,我试图将 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
最佳答案
o11c 已经解决了这个问题:
明确选择一种编码,例如"bob".encode('ascii')
.这似乎在 Python 3 中是必需的。
关于python - CFFI : TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, 不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683925/