简体   繁体   English

Android没有真正的wchar_t吗?

[英]Does Android not really have 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': 在函数'Java_com_example_hellojni_HelloJni_stringFromJNI'中:
warning: passing argument 1 of 'logDebugInfo' from incompatible pointer type 警告:从不兼容的指针类型传递'logDebugInfo'的参数1
note: expected 'wchar_t *' but argument is of type 'unsigned int *' 注意:预期'wchar_t *'但参数的类型为'unsigned int *'

And the resulting string was not correct. 结果字符串不正确。 If I removed that L prefix before that formatting string, weird, it worked. 如果我在格式化字符串之前删除了那个L前缀,很奇怪,它有效。 But L prefixes were used everywhere in my legacy code. 但是我的遗留代码中到处都使用了L前缀。

First I know wchar_t is not portable enough and is very compiler-specific. 首先我知道wchar_t不够便携,并且特定于编译器。 The size of wchar_t I expected was supposed to be 16 bits. 我期望的wchar_t的大小应该是16位。 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? 我读了一些其他帖子说它是32位的android而wchar.h,由官方NDK提供,它说wchar_t == char,真的吗?

From NDK r5b docs/STANDALONE-TOOLCHAIN.html: 来自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. 由于您的目标是Android 1.6,因此看起来好像wchar_t不适合您。

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. 即使在Android 2.3平台(“android-9”)中,仍有许多地方都有注释,包括wchar.h ,这意味着wchar_t是一个字节,并且没有实现任何宽字符库函数。 This suggests that the implementation may still be dodgy, so I would be very cautious about using wchar_t on any Android version. 这表明实现可能仍然是狡猾的,所以我会非常谨慎地在任何 Android版本上使用wchar_t。

If you're looking for an alternative, I have found UTFCPP to be an excellent and very lightweight library. 如果您正在寻找替代方案,我发现UTFCPP是一个优秀且非常轻量级的库。

This is a bit old, but I've hit this while searching for a solution. 这有点旧,但我在搜索解决方案时遇到了这个问题。
It seems that the NDK (r8d for me) is still not supporting wsprintf: see issue and code . 似乎NDK(对我来说是r8d)仍然不支持wsprintf:请参阅问题代码

In my case I am using libjson (considering switching to yajl) for iOS/Android shared native code. 在我的情况下,我使用libjson(考虑切换到yajl)用于iOS / Android共享本机代码。
Until I'll switch libraries, my workaround for NDK is this: 在我切换库之前,我对NDK的解决方法是这样的:

double value = 0.5; // for example
std::wstringstream wss;
wss << value;
return json_string(wss.str());

I've read that streams are slower than the C functions, and if you need a pure C (and not C++) solution it doesn't help, but maybe someone will find this useful. 我已经读过流比C函数慢,如果你需要一个纯C(而不是C ++)解决方案它没有帮助,但也许有人会觉得这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM