JNI 数据类型转换_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JNI 数据类型转换

JNI 数据类型转换

 2013/11/13 9:45:27  sea878412  程序员俱乐部  我要评论(0)
  • 摘要:#include<jni.h>#include"com_test_Test.h"#include<stdio.h>#include<stdlib.h>#include<strings.h>//获取字符串JNIEXPORTvoidJNICALLJava_com_test_Test_sayHello(JNIEnv*env,jobjectobj,jstrings){char*str=(char*)(*env)->GetStringUTFChars
  • 标签:数据类型 数据 类型转换
class="c">#include <jni.h>
#include "com_test_Test.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

//获取字符串
JNIEXPORT void JNICALL Java_com_test_Test_sayHello(JNIEnv * env, jobject obj,
  jstring s) {

 char * str = (char *) (*env)->GetStringUTFChars(env, s, 0);
 printf("\n c-string: hello - %s", str);

}

//获取,返回int
JNIEXPORT jint JNICALL Java_com_test_Test_add(JNIEnv * env, jobject obj,
  jint a, jint b) {
 int i = a;
 int j = b;
 printf("\n c-int: %d - %d", i, j);
 char str[256];
 sprintf(str, "%d", i + j);
 return (jint) i + j;
}

//获取,返回float
JNIEXPORT jfloat JNICALL Java_com_test_Test_getFloat(JNIEnv * env, jobject obj,
  jfloat f) {
 float fl = f;
 printf("\n c-float: %3f", fl);

 fl = 200.555;
 return (jfloat) fl;
}

//获取,返回 double
JNIEXPORT jdouble JNICALL Java_com_test_Test_getDouble(JNIEnv * env,
  jobject obj, jdouble dou) {
 double d = dou;
 printf("\n c-double: %3f", d);

 d = 800.88;
 return (jdouble) d;
}

//获取,返回boolean
JNIEXPORT jboolean JNICALL Java_com_test_Test_getBoolean(JNIEnv * env,
  jobject obj, jboolean bool) {

 unsigned char b = bool;
 printf("\n c-boolean: %lu  ", b);
 if (b) {
  printf("true");
 } else {
  printf("false");
 }

 b = 1;
 return (jboolean) b;
}

//获取,返回 string
JNIEXPORT jstring JNICALL Java_com_test_Test_getString(JNIEnv * env,
  jobject obj, jstring s) {
 char * st = (char *) (*env)->GetStringUTFChars(env, s, 0);
 printf("\n c-string:  %s", st);

 char * str = "hello wang wu!";
 jstring rtn;
 rtn = (*env)->NewStringUTF(env, str);
 return rtn;
}

//获取,返回 string []
JNIEXPORT jobjectArray JNICALL Java_com_test_Test_getStringArray(JNIEnv * env,
  jobject obj, jobjectArray arr) {
 int len = (*env)->GetArrayLength(env, arr);
 printf("\n c-stringArray :");
 int i = 0;
 for (i = 0; i < len; i++) {
  jobject obj = (*env)->GetObjectArrayElement(env, arr, i);
  jstring str = (jstring) obj;
  const char * szStr = (*env)->GetStringUTFChars(env, str, 0);
  printf(" %d-%s ", i, szStr);
  (*env)->ReleaseStringChars(env, str, szStr);
 }
 //-----返回----

 jstring str;
 jobjectArray args = 0;
 jsize size = 5;
 char* sa[] = { "Hello,", "world!", "zhang", "san", "yuang" };
 int j = 0;
 jclass objClass = (*env)->FindClass(env, "java/lang/String");
 args = (*env)->NewObjectArray(env, size, objClass, 0);
 for (j = 0; j < size; j++) {
  str = (*env)->NewStringUTF(env, sa[j]);
  (*env)->SetObjectArrayElement(env, args, j, str);
 }
 return args;
}

?中文乱码问题解决方案

?

/*
  * javactransfer.c
  * By dpwu
  * e-mail:dpwu_js@sina.com.cn
  */
  #include <windows.h>
  #include "javactransfer.h"//通过javah –jni javactransfer 生成
  #include <stdio.h>
  #include "stdlib.h"
  #include "string.h"
  char* jstringToWindows( JNIEnv *env, jstring jstr );
  jstring WindowsTojstring( JNIEnv* env, char* str );
  JNIEXPORT jstring JNICALL
  Java_javactransfer_hypotenuse0(JNIEnv *env,jobject obj,jstring send_buf,jstring recv_buf,jint errno)
  {
   char * Buf_Return;
   Buf_Return = (char*)malloc(1024);
  
   const char * recvtest = jstringToWindows( env, recv_buf );
  /*上句recvtest接收Java传过来的中文字符串正确,如果改为下句,则出现乱码:
  const char * recvtest = (*env)->GetStringUTFChars( env,recv_buf,0);
  */
  printf( " c recv :[%s]\n" , recvtest);
  
   sprintf(Buf_Return,"接收中文chinese!");
   printf( "\n\n\n c send :[%s]\n" , Buf_Return);
   recv_buf = WindowsTojstring(env ,Buf_Return);
  /*上句recv_buf传windows本地C中含有中文字符串给Java正确;如果改为下句出现乱码:
  recv_buf = (*env)->NewStringUTF( env, Buf_Return );
   */
   return recv_buf;
  }
  char* jstringToWindows( JNIEnv *env, jstring jstr )
  {
   int length = (*env)->GetStringLength(env,jstr );
   const jchar* jcstr = (*env)->GetStringChars(env,jstr, 0 );
   char* rtn = (char*)malloc( length*2+1 );
   int size = 0;
   size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
   if( size <= 0 )
   return NULL;
   (*env)->ReleaseStringChars(env,jstr, jcstr );
   rtn[size] = 0;
   return rtn;
  }
  jstring WindowsTojstring( JNIEnv* env, char* str )
  {
   jstring rtn = 0;
   int slen = strlen(str);
   unsigned short* buffer = 0;
   if( slen == 0 )
   rtn = (*env)->NewStringUTF(env,str );
   else
   {
   int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
   buffer = malloc( length*2 + 1 );
   if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
   rtn = (*env)->NewString( env, (jchar*)buffer, length );
   }
   if( buffer )
   free( buffer );
   return rtn;
  }

?

发表评论
用户名: 匿名