We all know that jstring corresponds to the java String, but what is the relationship between jstring and wchar_t? This is a very tangled issue. The jni character processing in a recent project uses wchar_t, the first idea to communicate with java is Baidu. You can find the jchar * that can be obtained through GetStringLengthGetStringChars. For details, see jchar defines 16 bits in double byte, oh, yeah, wchar_t is also a dual character (the habit of Windows). This is a good solution to directly convert the Code directly according to the Code provided on the Internet, and then memcpy looks like it will be solved in minutes. Source code: copy the Code 1 wchar_t * w2js (JNIEnv * env, jstring str) 2 {3 int len = env-> GetStringLength (str ); 4 wchar_t * w_buffer = new wchar_t [len]; 5 memset (w_buffer, 0, len + 1); 6 w_buffer [len] = '\ 0'; 7 wcsncpy (w_buffer, (wchar_t *) env-> GetStringChars (str, 0), len); 8 env-> ReleaseStringChars (str, (const unsigned short *) w_buffer); 9 return w_buffer; 10} 11 the copy code test was indeed so normal, and then tested some good strings and English characters are normal, no problem. so I started to test Chinese. Try 15 characters and debug the code to get the length of 15 normal, after the conversion, the length will become 11, Nima, was truncated. this is because the wcsncpy concept should be normal, but in fact there is indeed a problem, occasionally normal, occasionally problems Lack of characters. next we made a simple test sizeof (jchar) \ sizeof (wchar_t) to find that the wchar_t of android is completely different from that of jchar, one of which is 2 bytes and one 4 bytes, it is estimated that there is a problem during the copy process. due to lack of experience in character processing, the basic knowledge is not solid, and it is difficult to do it. After a whole day of tangle, there is no solution. Two days have passed, baidu accidentally processed another piece of code by using pointers as arrays. In fact, it finally solved the problem through this method. the processing process is roughly as follows: java-> String [UTF-16 (fixed double byte)]-> jstring-> jchar [fixed double byte]-> wchar_t [fixed four byte] Double Byte How to deal with the four bytes? Let's take a look at the memory data dual-character 0x11 0x12 four bytes 0x11 0x12 0x00 0x00 after reading the memory data, we should understand what's going on. right! ~ We only need to create a wchar_t * with the same length as jchar *, and then treat both of them as an array to process jchar * Each element's dual-byte wchar_t * Each element's four bytes (and use first two bytes) we can get the following code copy code 1 // jstring converted to wchar_t 2 // env: JNIEnv jni operations indispensable 3 // jstr: jstring source character (from java) 4 // dst: The converted result. The four-byte wchar_t seems to be dedicated to linux 5 // return: No 6 void js2w (JNIEnv * env, jstring jstr, wchar_t * dst) 7 {8 // get the java String Length 9 jsize jstr_len = env-> GetStringLength (jstr ); 10 // obtain the jchar pointer of the java string 11 const jchar * pjstr = env-> GetStringChars (jstr, 0); 12 13 tc_char * ptmp = new tc_char [jstr_len + 1]; 14 memset (ptmp, 0, sizeof (tc_char) * (jstr_len + 1); 15 // convert jchar to wchar_t16 for (int I = 0; I <jstr_len; I ++) 17 memcpy (& ptmp [I], & pjstr [I], 2); 18 19 wcscpy (dst, ptmp ); 20 delete [] ptmp; 21} copy the code and then wchar_t to convert the jstring principle is only a reverse process, directly copy code 1 // wchar_t to jstring 2 // env: JNIEnv jni operations indispensable 3 // src: wchar_t source character four bytes seems linux-specific 4 // return: after the conversion is completed, the result jstring 5 jstring w2js (JNIEnv * env, wchar_t * src) 6 {7 int src_len = wcslen (src); 8 jchar * dest = new jchar [src_len + 1]; 9 memset (dest, 0, sizeof (jchar) * (src_len + 1); 10 11 for (int I = 0; I <src_len; I ++) 12 memcpy (& dest [I], & src [I], 2); 13 jstring dst = env-> NewString (dest, src_len); 14 delete [] dest; 15 return dst; 16} copy the Code itself. This conversion is very simple, because I did not understand the nature of these two types, I had to take a lot of detours, but I learned a lot in the process of solving the problem.