上一篇写了输入和输出string。
那么输入binary bytes array应该怎么做呢?根据文档说明:
http://www.swig.org/Doc3.0/Library.html#Library_nn10
9.3.2 Passing binary data
If you have a function that expects binary data,
size_t parity(char *str, size_t len, size_t initial);you can wrap the parameters (char *str, size_t len) as a single argument using a typemap. Just do this:
%apply (char *STRING, size_t LENGTH) { (char *str, size_t len) }; ... size_t parity(char *str, size_t len, size_t initial);Now, in the target language, you can use binary string data like this:
>>> s = "H\x00\x15eg\x09\x20" >>> parity(s, 0)In the wrapper function, the passed string will be expanded to a pointer and length parameter. The (char *STRING, int LENGTH) multi-argument typemap is also available in addition to (char *STRING, size_t LENGTH).
and
c -->
char * get_str_inputbytes(char * str1,int len)
{
char * odata=(char *)malloc(100);
sprintf(odata,"%s:%s","Header",str1);
return odata;
}
<--
i -->
%module tsm
%apply (char *STRING, int LENGTH) { (char * str1,int len) };
%{
//头文件
extern char * get_str(const char * str1);
extern char * get_str_addheader(char * str1);
extern char * get_str_inputbytes(char * str1,int len);
%}
%newobject get_str_addheader;
extern char * get_str(const char * str1);
extern char * get_str_addheader(char * str1);
extern char * get_str_inputbytes(char * str1,int len);
<--
测试结果:
可见只能输入str作为参数,但是可以接受16进制数据;继续深挖......(3)