简体   繁体   English

将jint转换为jstring

[英]converting jint to jstring

#include <stdio.h>
#include "Package_MyTester.h"

jstring Java_Package_MyTester_NMethod
 (JNIEnv *env, jobject obj, jint first, jint second) {
  jint result_i = first * second;
  jstring result;
  int x = 0;
  for(x=0;x<5;x++) {
      printf("%d",x);
  }

  return result;      
}

This program multiplies two jints. 这个程序乘以两个jints。 The result has to be in jstring. 结果必须是jstring。 Is there a way to convert jint to jstring ?. 有没有办法将jint转换为jstring?

You need to create a C buffer containing the result (using sprintf ) and then return the result of a NewStringUTF function: 您需要创建一个包含结果的C缓冲区(使用sprintf ),然后返回NewStringUTF函数的结果:

jstring Java_Package_MyTester_NMethod
 (JNIEnv *env, jobject obj, jint first, jint second) {
  jint result_i = first * second;
  char buf[64]; // assumed large enough to cope with result

  sprintf(buf, "%d", result_i);  // error checking omitted

  return (*env)->NewStringUTF(env, buf);      
}

See §3.2.3 of http://java.sun.com/docs/books/jni/html/objtypes.html 参见http://java.sun.com/docs/books/jni/html/objtypes.html的 §3.2.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM