简体   繁体   中英

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. The result has to be in jstring. Is there a way to convert jint to jstring ?.

You need to create a C buffer containing the result (using sprintf ) and then return the result of a NewStringUTF function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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