Does Android not really have wchar_t?(Android真的没有wchar_t吗?)
问题描述
I built a simple method like below
wchar_t buf[1024] = {};
void logDebugInfo(wchar_t* fmt, ...)
{
va_list args;
va_start(args, fmt);
vswprintf( buf, sizeof(buf), fmt, args);
va_end(args);
}
jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
logDebugInfo(L"test %s, %d..", L"integer", 10);
return (*env)->NewStringUTF(env, buf);
}
I got following warning
In function 'Java_com_example_hellojni_HelloJni_stringFromJNI':
warning: passing argument 1 of 'logDebugInfo' from incompatible pointer type
note: expected 'wchar_t *' but argument is of type 'unsigned int *'
And the resulting string was not correct. If I removed that L prefix before that formatting string, weird, it worked. But L prefixes were used everywhere in my legacy code.
First I know wchar_t is not portable enough and is very compiler-specific. The size of wchar_t I expected was supposed to be 16 bits. I read some other posts which said it's 32 bits for android but wchar.h, provided by official NDK, it said wchar_t == char, really?
From NDK r5b docs/STANDALONE-TOOLCHAIN.html:
5.2/ wchar_t support: - - - - - - - - - - - As documented, the Android platform did not really support wchar_t until Android 2.3. What this means in practical terms is that: - If you target platform android-9 or higher, the size of wchar_t is 4 bytes, and most wide-char functions are available in the C library (with the exception of multi-byte encoding/decoding functions and wsprintf/wsscanf). - If you target any prior API level, the size of wchar_t will be 1 byte and none of the wide-char functions will work anyway. We recommend any developer to get rid of any dependencies on the wchar_t type and switch to better representations. The support provided in Android is only there to help you migrate existing code.
Since you are targeting Android 1.6, it looks as if wchar_t is not suitable for you.
Even in the Android 2.3 platform ("android-9"), there are still notes in a number of places, including wchar.h
, which imply that wchar_t
is one byte and none of the wide character library functions are implemented. This suggests that the implementation may still be dodgy, so I would be very cautious about using wchar_t on any Android version.
If you're looking for an alternative, I have found UTFCPP to be an excellent and very lightweight library.
这篇关于Android真的没有wchar_t吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android真的没有wchar_t吗?
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01