4
#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;      
}

该程序将两个 jints 相乘。结果必须在 jstring 中。有没有办法将 jint 转换为 jstring ?

4

1 回答 1

6

您需要创建一个包含结果的 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);      
}

请参阅http://java.sun.com/docs/books/jni/html/objtypes.html的§3.2.3

于 2012-05-08T09:03:46.283 回答