Android没有真正的wchar_t吗?

前端之家收集整理的这篇文章主要介绍了Android没有真正的wchar_t吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我建立了一个简单的方法如下
  1. wchar_t buf[1024] = {};
  2. void logDebugInfo(wchar_t* fmt,...)
  3. {
  4. va_list args;
  5. va_start(args,fmt);
  6. vswprintf( buf,sizeof(buf),fmt,args);
  7. va_end(args);
  8. }
  9.  
  10. jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,jobject thiz )
  11. {
  12. logDebugInfo(L"test %s,%d..",L"integer",10);
  13. return (*env)->NewStringUTF(env,buf);
  14. }

我得到了以下警告

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 *’

结果字符串不正确.
如果我在格式化字符串之前删除了那个L前缀,很奇怪,它有效.但是我的遗留代码中到处都使用了L前缀.

首先我知道wchar_t不够便携,并且特定于编译器.我期望的wchar_t的大小应该是16位.我读了一些其他的帖子说它是32位的android而wchar.h,由官方NDK提供,它说wchar_t == char,真的吗?

解决方法

来自NDK r5b docs / STANDALONE-TOOLCHAIN.html:
  1. 5.2/ wchar_t support:
  2. - - - - - - - - - - -
  3.  
  4. As documented,the Android platform did not really support wchar_t until
  5. Android 2.3. What this means in practical terms is that:
  6.  
  7. - If you target platform android-9 or higher,the size of wchar_t is
  8. 4 bytes,and most wide-char functions are available in the C library
  9. (with the exception of multi-byte encoding/decoding functions and
  10. wsprintf/wsscanf).
  11.  
  12. - If you target any prior API level,the size of wchar_t will be 1 byte
  13. and none of the wide-char functions will work anyway.
  14.  
  15. We recommend any developer to get rid of any dependencies on the wchar_t type
  16. and switch to better representations. The support provided in Android is only
  17. there to help you migrate existing code.

由于您的目标是Android 1.6,因此看起来wchar_t不适合您.

即使在Android 2.3平台(“android-9”)中,仍有许多地方都有注释,包括wchar.h,这意味着wchar_t是一个字节,并且没有实现任何宽字符库函数.这表明实现可能仍然是狡猾的,所以我会非常谨慎地在任何Android版本上使用wchar_t.

如果您正在寻找替代方案,我发现UTFCPP是一个优秀且非常轻量级的库.

猜你在找的Android相关文章