将未签名的字符阵列转换为Jstring

发布于 2025-02-10 23:33:06 字数 1445 浏览 0 评论 0原文

我遇到了试图将未签名的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)
}

enter image description here

And sometimes I get 22 bytes, sometimes 23 ... though the expected result is always 20 bytes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2025-02-17 23:33:06

将字符串传递给newstringutf必须被终止。但是,您要通过的字符串并非终止终止,并且在第一个空NULL之前的字符串末端看起来有些垃圾。

我建议创建一个较大的数组,并在最后添加一个空终结器:

JNIEXPORT jstring JNICALL Java_com_example_demo_DemoClass_functionToCall
  (JNIEnv *env, jobject recv) {
  unsigned char unsigned_char_array[20 + 1]; // + 1 for null terminator
  function_to_call(unsigned_char_array);    
  unsigned_char_array[20] = '\0'; // add null terminator

  printf("result : %s (%ld chars)\n", unsigned_char_array, (int) sizeof unsigned_char_array);

  return (env)->NewStringUTF((char*) unsigned_char_array); // should work now
}

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:

JNIEXPORT jstring JNICALL Java_com_example_demo_DemoClass_functionToCall
  (JNIEnv *env, jobject recv) {
  unsigned char unsigned_char_array[20 + 1]; // + 1 for null terminator
  function_to_call(unsigned_char_array);    
  unsigned_char_array[20] = '\0'; // add null terminator

  printf("result : %s (%ld chars)\n", unsigned_char_array, (int) sizeof unsigned_char_array);

  return (env)->NewStringUTF((char*) unsigned_char_array); // should work now
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文