python - CFFI : TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, 不是 str

标签 python ffi python-cffi

使用 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/

相关文章:

python - ConfigParser 获取所有具有重复项的键

python - 将不同数据框中的 2 列与主键条件进行比较,无需合并

haskell - 在 Windows 上使用 HsLua 出现链接错误

c++ - 只要使用 ctypes 的 long 被截断,Python 整数就会传递给 C++ 函数

rust - 使用 cargo 和 rust 在 macOS for Linux 上交叉编译和链接动态库 (cdylib)

python - 为什么 cffi 比 numpy 快这么多?

python - 类型错误 : movement() missing 1 required positional argument: 'self'

python - Django - 使文件 I/O 线程安全

sockets - 在CFFI中使用标准库头

python - 如何使用 cffi 在 C 语言中嵌入返回字符串的 Python 函数?