gpt4 book ai didi

java - 使用 SWIG 将 signed char * 类型的结构成员转换为 Java 中的字节数组 (byte[])

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:23 25 4
gpt4 key购买 nike

我正在尝试将类型为 signed char * 的结构成员转换为 Java 中的字节数组。我有以下结构:

typedef struct {
signed char * content;
int contentLength;
} Foo;

我已经试过了:

%typemap(jni) signed char *content [ANY] "jbyteArray"
%typemap(jtype) signed char *content [ANY] "byte[]"
%typemap(jstype) signed char *content [ANY] "byte[]"
%typemap(javaout) signed char *content [ANY] {
return $jnicall;
}
%typemap(memberin) int contentLength [ANY] {
int length=0;
$1 = &length;
}

%typemap(out) signed char * content [ANY] {
$result = JCALL1(NewByteArray, jenv, length);
JCALL4(SetByteArrayRegion, jenv, $result, 0, length, $1);
}

但是没有结果。 Foo 的 getContent 方法具有以下签名:

SWIGTYPE_p_signed_char getContent();

我希望此方法返回 byte[]。有解决办法吗?

最佳答案

这非常接近您想要的。您不需要 [ANY] 因为数组的大小在 C 中不是“固定的”(它由 int 指定,但这不是其类型的一部分).

你可以让你的类型图与:

%module test

%typemap(jni) signed char *content "jbyteArray"
%typemap(jtype) signed char *content "byte[]"
%typemap(jstype) signed char *content "byte[]"
%typemap(javaout) signed char *content {
return $jnicall;
}

%typemap(out) signed char * content {
$result = JCALL1(NewByteArray, jenv, arg1->contentLength);
JCALL4(SetByteArrayRegion, jenv, $result, 0, arg1->contentLength, $1);
}

// Optional: ignore contentLength;
%ignore contentLength;

%inline %{
typedef struct {
signed char * content;
int contentLength;
} Foo;
%}

我可能在这里遗漏了一些东西,但我看不到比这更好的从 out 类型映射中获取“self”指针的方法 - arg$argnum 不起作用$self 也没有。没有任何其他类型映射可应用于此功能,这会有所帮助。

(请注意,您可能还想为 signed char * content 编写一个 memberin 或使其不可变。我很想 %ignore contentLength 也完全是成员)。

关于java - 使用 SWIG 将 signed char * 类型的结构成员转换为 Java 中的字节数组 (byte[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11965992/

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