Does Android really have no wchar_t?

I built a simple method as shown 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 the following warning

In the function "Java_com_example_hellojni_HelloJni_stringFromJNI":
warning: passing argument 1 of 'logDebugInfo' from an incompatible pointer type
note: expected 'wchar_t *', but the argument is of type "unsigned int *"

And the resulting string was incorrect. If I removed this L prefix before this format string, that would be weird. But L prefixes were used throughout my legacy code.

At first I know that wchar_t is not portable enough and is very specific to the compiler. The expected wchar_t should be 16 bits in size. I read some other posts that said it was 32 bits for android, but wchar.h provided by the official NDK, he said wchar_t == char, really?

+6
source share
2 answers

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 seems that wchar_t is not suitable for you.

Even on the Android 2.3 platform ("android-9"), there are still notes in a number of places, including wchar.h , which means that wchar_t is one byte, and none of the wide character library functions are implemented. This suggests that the implementation can still be dodgy, so I would be very careful to use wchar_t on any version of Android.

If you are looking for an alternative, I have found UTFCPP an excellent and very lightweight library.

+13
source

This is a bit outdated, but I hit it looking for a solution.
It seems that NDK (r8d for me) still does not support wsprintf: see issue and code .

In my case, I use libjson (considering switching to yajl) for generic iOS / Android based code.
Until I 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 read that threads are slower than C functions, and if you need a clean C solution (and not C ++), this will not help, but maybe someone will find it useful.

0
source

Source: https://habr.com/ru/post/885697/


All Articles