Java and C :: About JNI Chinese String Operation Summary
/ ** * * @ Author zostapo * * @ e-mail dertysang@263.net * * @ qq 1670967 * * /
[Problem simple background]
Programming using Java is sometimes necessary to use JNI to implement efficiency or other aspects. The problem that has to be facing in JNI is a Chinese issue (if you are foreigners, and don't care about China's construction, then you don't have to watch; or you already know how to solve ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~, you don't have to watch it), Java uses Unicode to support runtime, and the saving format of the class file is UTF-8. ~~~~~~~~~~~~~~~~~
Of course, you don't have problems with Pure Java, and other problems do not include today's topic (such as file saved characters or Chinese issues in web development). The problem is coming when we need to use Java and a local environment.
(1) How does Java deliver a Unicode string to a local environment (2) How to pass the local environment to the Java environment
Of course, Chinese, I only care about the conversion between Chinese, English is very good, because there is no problem, other languages are not concerned.
[Test environment description]
Here's the environment you use. (1) Windows2000 Professional / 512M DDR RAM / P4 2.0G (2) Sun JDK1.4.0 (3) VC 6.0 / SP5
[Test Procedure Description]
============================ Write the Java program to get the interface file ============================================================================================================================================================== ============== // jni.java
Public class jni {static {system.loadLibrary ("jni"); public native string getCHINESE (STRING STR); public static void main (string args []) {jni j = new jni (); system.err.println "[Return characters from the Native environment]" J.GetCHINESE ("Test")); CHAR CH = 'Test'; system.err.println ("[in the Java Environment] 'Uneiicode =" (int " CH);}}
Compile Java program% java_home% / bin / javac jni.java
Get interface file% java_home% / bin / javah jni
//jni.h
/ * DO NOT EDIT THIS FILE - it is machine generated * / # include
// # include "jni.h" jniexport jstring jnicall java_jni_getchinese (jnienv * env, jobject obj, jstring s) {jchar * newstring; jstring ret = 0; / * // - Test Part III - Newstring = New Jchar [1]; newstring [0] = 27979; // Chinese 'measure' Unicode Ret = env-> Newstring (NewString, 1); delete [] newstring; * /
// - Test the first part - wchar_t * p = L "test"; Printf ("/ n [in the native environment] 'measure' Unicode =% D", * p); newstring = (jchar *) P Ret = env-> Newstring (newstring, 1);
// - Test the second part - Const Jchar * JSTR; JBOOLAN COPY = '1';
JStr = env-> getStringChars (s, & copy); WPRINTF (L "/ N [From Java Environment Incompose Character]% S / N", JSTR); Env-> ReleaseStringChars (S, JSTR);
Return Ret;}
============================ run the test program ================================================================================================ ========= put the compiled DLL file in the appropriate position (will not know what is appropriate).
If you really don't know, tell you a simple way, look at the java.library.path attribute. Program operation results. % Java_home% / bin / java jni
[In the native environment] 'measure' unicode = 27979 [return characters from the Native environment] [in the Java environment] 'measure' unicode = 27979
============================ Interpretation test process ======================================================================================= ========= Since Java is a Unicode runtime, I am very lazy, I can't write the transcoding program between the word code and Unicode. Online, in fact, many Telnet client programs are brought by themselves. But we run in a Windows environment, of course, directly use his Unicode (UNIX environment).
(1) Confirm that we use Unicode support. Please read the tchar.h file carefully, no matter what you are tested or in order to improve your WINDOWS programming
. Types use Wchar_T as local characters Unicode support.
(2) I am familiar with the relevant function of the JNI interface about the UTF and Unicode operations.
(3) Take a closer look at the code above.