JNI中Jstring中文乱码解决方案

JNI中Jstring中文乱码解决方案

分类: Java&JNI;C混合编程 912人阅读 评论(2) 收藏 举报

Java code

  1. package zeph.example;  
  2. import java.io.IOException;  
  3. public class JstringExample {  
  4.     /** 
  5.      * @param Jstring For Chinese    
  6.      * @author Ben Zeph 
  7.      */  
  8.     public native String stringMethod(String text);  
  9.     public static void main(String[] args) throws IOException{  
  10.         // TODO Auto-generated method stub   
  11.         JstringExample Je = new JstringExample();  
  12.         System.loadLibrary("example");  
  13.         String str = "天灵灵,地灵灵,乱码乱码,消失吧!!!";  
  14.         System.out.println("the String in java is " + '"' + str + '"');  
  15.         System.out.println("the String return by C is "+Je.stringMethod(str));  
  16.     }  
  17. }  
 

C code example_h.h

  1. #include "jni.h"   
  2. #ifndef _Included_example_h    
  3. #define _Included_example_h    
  4. #ifdef __cplusplus    
  5. extern "C" {   
  6. #endif    
  7. JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *, jobject, jstring);  
  8. /*Java: stringMethod 
  9.  *Fuction: tranform a java string to c char* 
  10.  *Author: Ben Zeph 
  11.  */  
  12. #ifdef __cplusplus    
  13. }   
  14. #endif    
  15. #endif  
 

C code exampledll.cpp

  1. #include "example_h.h"   
  2. #include "jni.h"   
  3. #include <stdio.h>   
  4. #include <windows.h>   
  5. char* jstringToWindows( JNIEnv *env, jstring jstr )  
  6. {  
  7.   int length = env->GetStringLength(jstr);  
  8.   const jchar* jcstr = env->GetStringChars(jstr, 0);  
  9.   char* rtn = (char*)malloc(length*2+1);  
  10.   int size = 0;  
  11.   size = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL);  
  12.   if( size <= 0 )  
  13.     return NULL;  
  14.   env->ReleaseStringChars(jstr, jcstr);  
  15.   rtn[size] = 0;  
  16.   return rtn;  
  17. }  
  18. jstring WindowsTojstring( JNIEnv *env, const char* str )  
  19. {  
  20.   jstring rtn = 0;  
  21.   int slen = strlen(str);  
  22.   unsigned short* buffer = 0;  
  23.   if( slen == 0 )  
  24.     rtn = env->NewStringUTF(str);   
  25.   else  
  26.   {  
  27.     int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0);  
  28.     buffer = (unsigned short*)malloc(length*2 + 1);  
  29.     if( MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) >0)  
  30.       rtn = env->NewString((jchar*)buffer, length);  
  31.   }  
  32.   if(buffer)  
  33.   free(buffer);  
  34.   return rtn;  
  35. }  
  36. JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *env, jobject obj, jstring string)  
  37. {  
  38.     const char *str ;  
  39.     jstring tostring;  
  40.     str = jstringToWindows(env, string);  
  41.     printf("The string in C code %s/n",str);  
  42.     tostring = WindowsTojstring(env,str);  
  43.     return tostring;  
  44. }  
  45. void main(){}  
 

 

 

C中的两个函数

char* jstringToWindows( JNIEnv *env, jstring jstr )

jstring WindowsTojstring( JNIEnv *env, const char* str )

转载于其他文章,在此感谢其作者

 

代码中需要的两个头文件jni.h和jni_md.h

到这里下载http://download.csdn.net/source/3186188

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值