Does Android not really have wchar_t?

前端 未结 2 1276
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 05:58

I built a simple method like below

wchar_t buf[1024] = {};
void logDebugInfo(wchar_t* fmt, ...)
{  
    va_list args;
    va_start(args, fmt);
    vswprintf(         


        
相关标签:
2条回答
  • 2021-01-05 06:43

    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.
    

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

    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.

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

    0 讨论(0)
  • 2021-01-05 06:56

    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.

    In my case I am using libjson (considering switching to yajl) for iOS/Android shared native code.
    Until I'll switch libraries, my workaround for NDK is this:

    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.

    0 讨论(0)
提交回复
热议问题