Java and C ++: Summary of JNI Chinese character string operations
/**
*
* @ Author zosatapo
*
* @ E-mail dertyang@263.net
*
* @ Qq 1670967
*
*/
[Simple problem background]
Java programming sometimes requires JNI for efficiency or other issues. JNI has to face
The problem is Chinese. (If you are a foreigner and don't care about Chinese construction, you don't need to read it. Or you already know how to solve it.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you do not need to read), Java uses Unicode to support runtime, of course, class file storage format is UTF-8.
~~~~~~~~~~~~~~~~~
Of course, you will not encounter any problems when using pure Java, and other problems will not be included in today's topics (such as the character set stored in files ).
Or a Chinese issue in Web development ). When we need to use Java and local mixed programming, the problem arises:
(1) how to pass Unicode strings to the local environment in Java
(2) How to pass Unicode characters to the Java environment in the local environment
Of course, Chinese people only care about the conversion between Chinese characters. English is easy to handle, because there is no problem, and other languages do not even care.
[Test environment description]
The following describes my environment.
(1) Windows2000 Professional/512 m ddr ram/P4 2.0g
(2) sun jdk1.4.0
(3) VC ++ 6.0/SP5
[Test process description]
======================================
Compile a Java program to obtain 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] 'test' Unicode =" + (INT) CH );
}
}
Compile a Java program
% Java_home %/bin/javac JNI. Java
Obtain the interface file
% Java_home %/bin/javah JNI
// JNI. h
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class JNI */
# Ifndef _ included_jni
# DEFINE _ included_jni
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: JNI
* Method: getchinese
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
Jniexport jstring jnicall java_jni_getchinese
(Jnienv *, jobject, jstring );
# Ifdef _ cplusplus
}
# Endif
# Endif
======================================
Write local implementation
======================================
I don't need to talk about how to write DLL, so I am just simple
Extract the relevant code.
// # Include "JNI. H"
Jniexport jstring jnicall java_jni_getchinese
(Jnienv * ENV, jobject OBJ, jstring S)
{
Jchar * newstring;
Jstring ret = 0;
/*
// -- Test the third part --
Newstring = new jchar [1];
Newstring [0] = 27979; // Unicode of the Chinese 'test'
Ret = env-> newstring (newstring, 1 );
Delete [] newstring;
*/
// -- Test the first part --
Wchar_t * P = l "test ";
Printf ("/n [in the native environment] 'test' Unicode = % d", * P );
Newstring = (jchar *) P;
Ret = env-> newstring (newstring, 1 );
// -- Test the second part --
Const jchar * jstr;
Jboolean copy = '1 ';
Jstr = env-> getstringchars (S, & copy );
Wprintf (L "/n [input characters from the Java environment] % s/n", jstr );
Env-> releasestringchars (S, jstr );
Return ret;
}
======================================
Run the test program
======================================
Place the compiled DLL file in the appropriate location (I don't know what the appropriate location is ).
If you really don't know, tell you the simplest way to look at it.
The Java. Library. Path attribute is ready.
Program running result.
% Java_home %/bin/Java JNI
[In the native environment] 'test' Unicode = 27979
[[Return characters from native environment] Test
[In the Java environment] 'test' Unicode = 27979
======================================
Explain the test process
======================================
Since Java is running in UNICODE, I am very lazy and cannot write transcoding programs between bytecode and Unicode myself.
In fact, many Telnet client programs are self-contained. But we are running in windows.
Of course, his Unicode support is directly used (the Unix environment also has corresponding support ).
(1) confirm that we use Unicode support. Please read the tchar. h file carefully, whether it is for this test or to improve your understanding of Windows Programming
.
The Type uses wchar_t as the Unicode support for local characters.
(2) Familiarize yourself with the functions related to UTF and Unicode operations of the JNI interface.
(3) Check the above Code carefully.