python传递字符串到c或c++时,出现格式错误:TypeError: bytes or integer address expected instead of str instance
原因:因为编写的c或者c++中的编码并不是utf-8的,故当你在linux下调用该c或c++函数时就会出现类型不一致。当然,中文的编码时也应该变为utf8。
注:windows编码方式默认是gbk和linux默认是utf8
解决方案:
将传递的字符串改为utf8编码:
如,python下时转换为:"11.jpg".encode("utf-8")
(pah:0)root@anhui-k55vd:python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> so=ctypes.cdll.LoadLibrary("./libcrachrecg.so")
>>> so.testGetImg(ctypes.c_char
ctypes.c_char( ctypes.c_char_p(
>>> so.testGetImg(ctypes.c_char
ctypes.c_char( ctypes.c_char_p(
>>> so.testGetImg(ctypes.c_char_p("./11.jpg"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bytes or integer address expected instead of str instance
>>> so.testGetImg(ctypes.c_char_p("11.jpg"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bytes or integer address expected instead of str instance
>>> so.testGetImg("11.jpg".encode("utf-8"))
0
>>>
运行正确。
|