将未签名的字符阵列转换为Jstring
我遇到了试图将未签名的char阵列转换为Jstring的问题。
上下文是我正在使用Java的共享C库。 因此,我正在实现JNI C ++文件。 我从库中使用的函数retururs a num_carte_transcode
和我在JNI C ++文件中实现的功能返回JString。
因此,我需要将未签名的字符阵列转换为Jstring。
我尝试了这个简单的铸造return(env) - > newstringutf(((char*)unsigned_char_array);
,
但尽管数组只能包含20个字节,但我在Java中随机22或23个字节... (虽然20个字节是正确的)
edit1:在这里提供更多详细信息
JNIEXPORT jstring JNICALL Java_com_example_demo_DemoClass_functionToCall
(JNIEnv *env, jobject, ) {
// Here I define an unsigned char array as requested by the library function
unsigned char unsigned_char_array[20];
// The function feeds the array at execution
function_to_call(unsigned_char_array);
// I print out the result for debug purpose
printf("result : %.*s (%ld chars)\n", (int) sizeof unsigned_char_array, unsigned_char_array, (int) sizeof unsigned_char_array);
// I get the result I want, which is like: 92311221679609987114 (20 numbers)
// Now, I need to convert the unsigned char array to jstring to return to Java
return (env)->NewStringUTF((char*) unsigned_char_array);
// Though... On debugging on Java, I get 21 bytes 92311221679609987114 (check the image below)
}
,有时我会得到22个字节,有时23 ...尽管预期的结果总是20个字节。
I'm having issues trying to convert an unsigned char array to jstring.
The context is I'm using a shared c library from Java.
So I'm implementing a JNI c++ file.
The function I use from the library returs a unsigned char* num_carte_transcode
And the function I implemented in the JNI C++ file returns a jstring.
So I need to convert the unsigned char array to jstring.
I tried this simple cast return (env)->NewStringUTF((char*) unsigned_char_array);
But though the array should only contain 20 bytes, I get randomly 22 or 23 bytes in Java...
(Though the 20 bytes are correct)
EDIT1: Here some more details with example
JNIEXPORT jstring JNICALL Java_com_example_demo_DemoClass_functionToCall
(JNIEnv *env, jobject, ) {
// Here I define an unsigned char array as requested by the library function
unsigned char unsigned_char_array[20];
// The function feeds the array at execution
function_to_call(unsigned_char_array);
// I print out the result for debug purpose
printf("result : %.*s (%ld chars)\n", (int) sizeof unsigned_char_array, unsigned_char_array, (int) sizeof unsigned_char_array);
// I get the result I want, which is like: 92311221679609987114 (20 numbers)
// Now, I need to convert the unsigned char array to jstring to return to Java
return (env)->NewStringUTF((char*) unsigned_char_array);
// Though... On debugging on Java, I get 21 bytes 92311221679609987114 (check the image below)
}
And sometimes I get 22 bytes, sometimes 23 ... though the expected result is always 20 bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将字符串传递给
newstringutf
必须被终止。但是,您要通过的字符串并非终止终止,并且在第一个空NULL之前的字符串末端看起来有些垃圾。我建议创建一个较大的数组,并在最后添加一个空终结器:
The string passed to
NewStringUTF
must be null-terminated. The string you're passing, however, is not null-terminated, and it looks like there's some garbage at the end of the string before the first null.I suggest creating a larger array, and adding a null terminator at the end: