Using the CFFI library for Python, I'm trying to coax a Python string into a char*, so that I can pass it to a C function that accepts char*. I can't seem to figure out what the right incantation is.
Consider this example:
>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.new("char[]", "bob")
The result is:
TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str
The following does not work either:
>>> ffi.new("char*", "bob")
It says:
TypeError: initializer for ctype 'char' must be a bytes of length 1, not str
o11c solved this already:
Pick explicitly an encoding, e.g. "bob".encode('ascii')
. This seems to be just necessary in Python 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With