android wchar t 中文,Android JNI - Reliable way to convert jstring to wchar_t

One way would be to use String.getBytes("UTF-32LE"). Note this is making the ASSUMPTION that wchar_t is 4 bytes and little-endian, but this should be a fairly safe assumption to make.

Here's an example that passes a String from Java to C++, where it is converted to std::wstring, reversed, and passed back to Java: class MyClass { private native byte[] reverseString(byte[] arr); String reverseString(String s) { try { return new String(reverseString(s.getBytes("UTF-32")), "UTF-32"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } }

On the C++ side you have: std::wstring toWStr(JNIEnv *env, jbyteArray s) { const wchar_t *buf = (wchar_t*) env->GetByteArrayElements(s, NULL); int n = env->GetArrayLength(s) / sizeof(wchar_t); // First byte is BOM (0xfeff), so we skip it, hence the "buf + 1". // There IS NO null-terminator. std::wstring ret(buf + 1, buf + n); env->ReleaseByteArrayElements(s, (jbyte*) buf, 0); return ret; } jbyteArray fromWStr(JNIEnv *env, const std::wstring &s) { jbyteArray ret = env->NewByteArray((s.size()+1)*sizeof(wchar_t)); // Add the BOM in front. wchar_t bom = 0xfeff; env->SetByteArrayRegion(ret, 0, sizeof(wchar_t), (const jbyte*) &bom); env->SetByteArrayRegion(ret, sizeof(wchar_t), s.size()*sizeof(wchar_t), (const jbyte*) s.c_str()); return ret; } extern "C" JNIEXPORT jbyteArray JNICALL Java_MyClass_reverseString(JNIEnv *env, jobject thiz, jbyteArray arr) { std::wstring s= toWStr(env, arr); std::reverse(s.begin(), s.end()); return fromWStr(env, s); }

I tested it both on my phone, which has Android 4.1.2 and ARM CPU, and on the Android Emulator - Android 4.4.2 and x86 CPU, and this code: MyClass obj = new MyClass(); Log.d("test", obj.reverseString("hello, здравствуйте, 您好, こんにちは"));

Gave this output: 06-04 17:18:20.605: D/test(8285): はちにんこ ,好您 ,етйувтсвардз ,olleh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值