2012-05-08 64 views
2
#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?將jint轉換爲jstring

+0

§3.2.3爲什麼會出現Java的' '標籤? –

+2

@BhavikAmbani,因爲JNI是Java本地接口 – Thomas

+0

@Thomas好吧,我知道了。謝謝 –

回答

4

您需要創建一個包含結果的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