安卓系统真的没有wchar_t吗?

 2023-01-20    301  

问题描述

我构建了一个简单的方法,例如

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);
}

我遵循警告

安卓系统真的没有wchar_t吗?

在函数中
警告:从不兼容的指针类型
传递” logdebuginfo”的参数1
注意:预期的’wchar_t *’但是参数是类型’unsigned int *’

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

首先,我知道WCHAR_T不够便携,并且非常针对编译器.我期望的WCHAR_T的大小应该是16位.我读了其他一些帖子,这些帖子说是Android的32位,但是由官方NDK提供的WCHAR.H说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是一个字节,并且没有一个宽字符库函数是实施的.这表明该实现仍然可能是狡猾的,因此我对在任何 aincon Android版本上使用WCHAR_T非常谨慎.

如果您正在寻找替代方案,我发现 utfcpp 是一个很棒且非常轻巧的库.

其他推荐答案

这有点旧,但是我在搜索解决方案时就达到了这一点.

看来NDK(对我来说R8D)仍然不支持WSPRINTF:
请参阅 esseage
and 代码.
.

就我而言,我正在使用ios/android共享本机代码的libjson(考虑切换到yajl).
在我切换库之前,我的NDK解决方法是:

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

我读过,流比C函数慢,如果您需要纯C(而不是C ++)解决方案,则无济于事,但也许有人会发现这有用.

以上所述是小编给大家介绍的安卓系统真的没有wchar_t吗?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26121.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。