gpt4 book ai didi

python - SWIG C/python 和无符号字符指针

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:53 25 4
gpt4 key购买 nike

我在表单中有一个函数

void f(unsigned char *out, const unsigned long long outlen,
const unsigned char *in, const unsigned long long inlen);

当我尝试使用它时:

data_in = ['a', 'b', 'c', 'd', 'e']
data_out = [0]*100
f(data_out, len(data_out), data_in, len(data_in))

(其实我更想传bytearrays)

我得到类似的东西:

Traceback (most recent call last):
File "xxxx/basic_test.py", line 6, in <module>
f(data_out, len(data_out), data_in, len(data_in))
TypeError: in method 'f', argument 1 of type 'unsigned char *'

我尝试了不同的方式来传递 data_out(编码、byterray、[0]*100 等),但似乎没有任何效果...

这应该如何工作?

为了以防万一,这可能会产生一些影响,这是一个 C 函数,所以为了避免混淆,我用

包装了它
extern "C"
{
...
}

很遗憾,我无法更改 C 代码

最佳答案

您的 python 字符串似乎与 C 函数的预期参数类型不兼容。 this可能会帮助你。它建议使用 c_char_p 来获得正确的类型。也就是说,如果您想在不复制字符串的情况下执行此操作,但有一些限制。

如果你可以随意定义字符串,或者复制它,有ctypes,其中一种是c_ubyte

关于python - SWIG C/python 和无符号字符指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46029231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com