写Java JNI的时候最头痛的莫过于字符集的转换问题,众所周知,Java里字符串是以Unicode编码存储的,而C/C++里大部分是单字节处理,这就涉及到一个转换的问题,下面是两个函数用来在JString到Char*之间的转换:
//author Naven 2003 for both Win32 and Unix os //为了便于使用,无需另连接库,Win32里需自己写双字节memset函数 #if defined(_WIN32) || defined(__WIN32__) wchar_t *mywmemset(wchar_t *_S, wchar_t _C, size_t _N) { wchar_t *_Su = _S; for (; 0 < _N; ++_Su, --_N) *_Su = _C; return (_S); } #endif //将Java里jstring类型转换成单字节字符串 size_t JStringToChars(JNIEnv *env, const jstring jstr, char *mbstr, const size_t mbslen) { const jchar *jwstr; wchar_t *wbuf=0; size_t nsize=0; jsize jlen=0; register int i=0; memset(mbstr, 0, mbslen); if( jstr == NULL ) return 0; jwstr = (*env)->GetStringChars(env, jstr, 0); jlen = (*env)->GetStringLength(env, jstr); if( jlen <= 0 ) return 0; #if defined(_WIN32) || defined(__WIN32__) wbuf = (wchar_t *) jwstr; nsize = wcstombs(mbstr, wbuf, mbslen-1); #else wbuf = (wchar_t *) malloc((jlen+1)*sizeof(wchar_t)); mywmemset(wbuf, (wchar_t)0, (size_t)(jlen+1)); for( i = 0; i < jlen; i ++ ) wbuf[i] = (wchar_t)jwstr[i]; nsize = wcstombs(mbstr, wbuf, mbslen-1); free(wbuf); #endif if( nsize >= 0 && nsize < mbslen ) mbstr[nsize] = 0; return nsize; } //将单字节字符串转换成Java里可使用的jstring类型 size_t CharsToJString(JNIEnv *env, const char *mbstr, jstring *jstr) { jchar *jwstr=0; wchar_t *wcstr=0; size_t slen=0, nsize=0; register int i=0; if( mbstr != NULL ) slen = strlen(mbstr); if( mbstr == NULL || slen <= 0 ) { *jstr = (*env)->NewStringUTF(env, ""); return 0; } wcstr = (wchar_t *) malloc((slen+1)*sizeof(wchar_t)); mywmemset(wcstr, 0, slen+1); nsize = mbstowcs(wcstr, mbstr, slen*2); jwstr = (jchar *) wcstr; #if defined(_WIN32) || defined(__WIN32__) #else for(i =0; i < (int)nsize; i ++) { jwstr[i] = jwstr[i*2]; } jwstr[nsize] = 0; #endif if( nsize <= 0 ) *jstr = (*env)->NewStringUTF(env, ""); else *jstr = (*env)->NewString(env, jwstr, nsize); free(wcstr); return nsize; }