Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python swig c++;通过引用返回无符号字符*_Python_C++_Swig_Unsigned Char - Fatal编程技术网

Python swig c++;通过引用返回无符号字符*

Python swig c++;通过引用返回无符号字符*,python,c++,swig,unsigned-char,Python,C++,Swig,Unsigned Char,我使用SWIG将C++函数包到Python中: int getBytes(unsigned char *& result, int length) 我学习了如何包装通过引用返回的char*函数: %typemap(in,numinputs=0) char*& (char* tmp) %{ $1 = &tmp; %} %typemap(argout) char*& (PyObject* obj) %{ obj = PyUnicode_FromSt

我使用SWIG将C++函数包到Python中:
int getBytes(unsigned char *& result, int length)
我学习了如何包装通过引用返回的char*函数:

%typemap(in,numinputs=0) char*& (char* tmp) %{
    $1 = &tmp;
%}

%typemap(argout) char*& (PyObject* obj) %{
    obj = PyUnicode_FromString(*$1);
    $result = SWIG_Python_AppendOutput($result,obj);
%}

%typemap(freearg) char*& %{
    free(*$1);
%}
我试图将其应用于无符号字符*,但失败,并得到错误信息,即
SWIG\u Python\u AppendOutput
无法将无符号字符**转换为有符号字符**。我搜索了python文档,没有找到可以将无符号字符**转换为无符号字符**的函数。
有人能帮忙吗?谢谢

我很久以前就解决了这个问题,下面是我的解决方案:

%typemap(in, numinputs= 0) (unsigned char *&result, int &length) (unsigned char *temp, int len) %{
  $1 = &temp;
  $2 = &len;
%}

%typemap(argout) (unsigned char *&result, int &length)  %{
    PyObject* arg5 = 0;
    arg5 = SWIG_FromCharPtrAndSize((const char *)(*$1), *$2);

    PyObject* temp = NULL;
    temp = $result;
    $result = PyList_New(1);
    PyList_SetItem($result, 0, temp);
    PyList_Append($result, (PyObject*)arg5);//unsigned char *&
    PyList_Append($result, PyInt_FromLong(*$2));

    Py_DECREF(temp);
%}