问题
I'm trying to port some old C/C++ code to android(latest version of NDK), but I'm struggling with function wcstombs(). This function returns wierd result on Android and I don't know why.
Look at this code (it's wcstombs() example from C++ reference)
const wchar_t str[] = L"wcstombs example";
char buffer[32];
int ret;
ret = wcstombs ( buffer, str, sizeof(buffer) );
if (ret==32) buffer[31]='\0';
__android_log_print(ANDROID_LOG_WARN, "dbg", "%d ... %s", ret, buffer);
// Application.mk
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
APP_PLATFORM := android-19
This code prints "2 ... w". It looks like the function stops, if it reads a zero byte from the string.
Is it normal? Is there any replacement for this function?
Thx.
回答1:
you could use
snprintf(buffer, sizeof(buffer), "%ls", str);
回答2:
I had this same issue. Changing the STL library did not work for me.
However using this implementation did work.
https://android.googlesource.com/platform/ndk/+/master/sources/android/support/src/musl-multibyte/wcsrtombs.c
There is also crystax ndk, which is a replacement for the google ndk. It could be useful.
https://www.crystax.net
回答3:
I've finally found the solution to this problem. Application has to be linked against LLVM libc++ instead of GNU STL. Application.mk should look like this:
APP_ABI := armeabi-v7a
APP_STL := c++_static
APP_PLATFORM := android-19
来源:https://stackoverflow.com/questions/25453867/wcstombs-has-invalid-output-on-android