android jni char* jstring 乱码,JNI中Jstring中文乱码解决方案
Java code[java] view plain copy print ?packagezeph.example;importjava.io.IOException;publicclassJstringExample{/***@paramJstringForChinese*@authorBenZeph*/publicnativeStringstringMethod(Strin...
Java code
[java] view plain copy print ?
packagezeph.example;
importjava.io.IOException;
publicclassJstringExample {
/**
* @param Jstring For Chinese
* @author Ben Zeph
*/
publicnativeString stringMethod(String text);
publicstaticvoidmain(String[] args)throwsIOException{
// TODO Auto-generated method stub
JstringExample Je =newJstringExample();
System.loadLibrary("example");
String str ="天灵灵,地灵灵,乱码乱码,消失吧!!!";
System.out.println("the String in java is "+'"'+ str +'"');
System.out.println("the String return by C is "+Je.stringMethod(str));
}
} package zeph.example; import java.io.IOException; public class JstringExample { /** * @param Jstring For Chinese * @author Ben Zeph */ public native String stringMethod(String text); public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub JstringExample Je = new JstringExample(); System.loadLibrary("example"); String str = "天灵灵,地灵灵,乱码乱码,消失吧!!!"; System.out.println("the String in java is " + '"' + str + '"'); System.out.println("the String return by C is "+Je.stringMethod(str)); } }
C code example_h.h
[cpp] view plain copy print ?
#include "jni.h"
#ifndef _Included_example_h
#define _Included_example_h
#ifdef __cplusplus
extern"C"{
#endif
JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *, jobject, jstring);
/*Java: stringMethod
*Fuction: tranform a java string to c char*
*Author: Ben Zeph
*/
#ifdef __cplusplus
}
#endif
#endif #include "jni.h" #ifndef _Included_example_h #define _Included_example_h #ifdef __cplusplus extern "C" { #endif JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *, jobject, jstring); /*Java: stringMethod *Fuction: tranform a java string to c char* *Author: Ben Zeph */ #ifdef __cplusplus } #endif #endif
C code exampledll.cpp
[cpp] view plain copy print ?
#include "example_h.h"
#include "jni.h"
#include
#include
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
intlength = env->GetStringLength(jstr);
constjchar* jcstr = env->GetStringChars(jstr, 0);
char* rtn = (char*)malloc(length*2+1);
intsize = 0;
size = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL);
if( size <= 0 )
returnNULL;
env->ReleaseStringChars(jstr, jcstr);
rtn[size] = 0;
returnrtn;
}
jstring WindowsTojstring( JNIEnv *env,constchar* str )
{
jstring rtn = 0;
intslen = strlen(str);
unsignedshort* buffer = 0;
if( slen == 0 )
rtn = env->NewStringUTF(str);
else
{
intlength = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0);
buffer = (unsignedshort*)malloc(length*2 + 1);
if( MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) >0)
rtn = env->NewString((jchar*)buffer, length);
}
if(buffer)
free(buffer);
returnrtn;
}
JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *env, jobject obj, jstring string)
{
constchar*str ;
jstring tostring;
str = jstringToWindows(env, string);
printf("The string in C code %s/n",str);
tostring = WindowsTojstring(env,str);
returntostring;
}
voidmain(){} #include "example_h.h" #include "jni.h" #include #include char* jstringToWindows( JNIEnv *env, jstring jstr ) { int length = env->GetStringLength(jstr); const jchar* jcstr = env->GetStringChars(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(jstr, jcstr); rtn[size] = 0; return rtn; } jstring WindowsTojstring( JNIEnv *env, const char* str ) { jstring rtn = 0; int slen = strlen(str); unsigned short* buffer = 0; if( slen == 0 ) rtn = env->NewStringUTF(str); else { int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0); buffer = (unsigned short*)malloc(length*2 + 1); if( MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) >0) rtn = env->NewString((jchar*)buffer, length); } if(buffer) free(buffer); return rtn; } JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *env, jobject obj, jstring string) { const char *str ; jstring tostring; str = jstringToWindows(env, string); printf("The string in C code %s/n",str); tostring = WindowsTojstring(env,str); return tostring; } 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)