我有一个返回表示二进制数据的unsigned char*的C函数。我注意到在SWIG文档中有一个很好的typemap可以处理二进制数据作为输入到C函数,但是当C函数返回二进制数据且它是unsigned时怎么办?有什么建议吗?
swig.i:
%apply (char *STRING, size_t LENGTH) { (const char data[], size_t len) }
%inline %{
void binaryChar1(const char data[], size_t len) {
printf("len: %d data: ", len);
for (size_t i=0; i<len; ++i)
printf("%x ", data[i]);
printf("\n");
}
%}
java:
byte[] data = "hi\0jk".getBytes();
example.binaryChar1(data);
C示例:
enw_resultrow_t *result_row = getResultRow();
unsigned char *blob;
while ((blob = getBinaryFromRow(result_row, &length))) {
char fname[32];
FILE *fp;
i++;
snprintf (fname, sizeof(fname), "FileXYZ", i);
printf ("Blob from %d:%s is saved in %s has %d bytes\n", i,
aSender?inet_ntoa(aSender->sin_addr):"???", fname, length);
if ((fp = fopen (fname, "w"))) {
l = fwrite (blob, sizeof (unsigned char), length, fp);
printf("Successfully wrote %d bytes to file\n", l);
fclose (fp);
} else {
printf("Error writing file");
}
}
unsigned char *getData(size_t *out_length); // 将返回数据的大小存储在out_length中
。 - Flexo