C中的函数如下所示:
char* test(const char* input, size_t length);使用swig(3.0.2)导出到python扩展。
在python2中,运行test("hello",len("hello")),该函数运行得很好。
但在Python3中,运行config="hello";test(config.encode("utf-8"),len(config.encode("utf-8")))
TypeError: in method 'test', argument 0 of type 'char const *'如何在Python3中使用此函数?
发布于 2015-05-11 14:58:20
我找出了真正的原因。
在Python3中,我应该使用与python2中相同的东西。这就是说test("hello",len("hello"))
但是Python3 the len("hello")并不适用于真正的UTF-8。
例如,
python2:
>>>len("测试")
4python3:
>>>len("测试")
2所以C函数的长度是错误的。
发布于 2015-05-10 19:16:28
在C中,const char *是不可变字符串( char * )的类型。我不确定你为什么会得到这个错误--可能是因为你正在传递一个Python字符串,这个字符串是可变的--你可以修改/重新分配它:
config = "hello"
config = config[:2]https://stackoverflow.com/questions/30150486
复制相似问题