难道Android的不是真的有wchar_t的? [英] Does Android not really have wchar_t?

查看:409
本文介绍了难道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

在功能Java_com_example_hellojni_HelloJni_stringFromJNI:
  警告:从不兼容的指针类型
过客logDebugInfo的说法1   注:应为为wchar_t *,但参数的类型为无符号整型*

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 preFIX,怪异,它的工作。但是大号prefixes到处都用我的旧的code。

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.

首先,我知道wchar_t的不够便携,很编译器相关的。对wchar_t的我预期的大小应该是16位。我读这表示它是32位的Andr​​oid,但wchar.h,官方NDK提供了一些其他职位,它说wchar_t的==炭,是真的吗?

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?

推荐答案

从NDK R5B文档/ STANDALONE-TOOLCHAIN​​.html:

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.

既然你是针对Android 1.6的,看起来好像wchar_t的是不适合你。

Since you are targeting Android 1.6, it looks as if wchar_t is not suitable for you.

即使是在安卓2.3平台(Android为9​​),仍然有注意到一些地方,包括 wchar.h ,这意味着 wchar_t的是一个字节,并没有任何的宽字符库函数来实现。这表明,实施可能仍是狡猾的,所以我会非常谨慎使用wchar_t的上的任意的Andr​​oid版本。

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.

如果你正在寻找一个替代方案,我发现 UTFCPP 是一款优秀且非常轻便的库。

If you're looking for an alternative, I have found UTFCPP to be an excellent and very lightweight library.

这篇关于难道Android的不是真的有wchar_t的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆