Android没有真正的wchar_t吗?

前端之家收集整理的这篇文章主要介绍了Android没有真正的wchar_t吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我建立了一个简单的方法如下
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);
}

我得到了以下警告

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:
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.

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

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

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

原文链接:https://www.f2er.com/android/317144.html

猜你在找的Android相关文章