UTF8使用NDK进行UTF16转换

时间:2016-02-04 06:00:12

标签: java c++ utf-8 android-ndk java-native-interface

我正在开发一个Android应用程序,它有很多c ++(Native)代码,因此我使用JNI与应用程序的Java部分进行交互。现在我想在我的本机代码中将我的字符串从UTF8转换为UTF16,反之亦然。 为此我使用JNI函数。

以下是从UTF8转换为UTF16的代码。

//Create New jString using the utf8 string.

jstring jStr = env->NewStringUTF(str.c_str());

//Now we have a jstring which keeps the data in unicode format 
// so get the UTF16 data from the JString.

const jchar *utf16Raw = env->GetStringChars(jStr, JNI_FALSE);

if (utf16Raw != nullptr) {
   //Use Unicode data
} 

但我怀疑这个逻辑是否正确,还是我需要使用JNI将jstring移动到JAVA端并将jstring从utf8转换为utf16?

0 个答案:

没有答案