Android真的没有wchar\t吗?
我构建了一个简单的方法,如下所示Android真的没有wchar\t吗?,android,c,java-native-interface,android-ndk,Android,C,Java Native Interface,Android Ndk,我构建了一个简单的方法,如下所示 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( J
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);
}
我得到以下警告
在函数“Java_com_example_hellojni_hellojni_stringFromJNI”中:警告:从不兼容的指针类型传递“logDebugInfo”的参数1
注意:应为“wchar\u t*”,但参数的类型为“unsigned int*” 结果字符串不正确。 如果我在格式化字符串之前删除了L前缀,奇怪的是,它起作用了。但是我的遗留代码中到处都使用了L前缀
首先,我知道wchar\u t不够可移植,并且非常特定于编译器。我预期的wchar\u t的大小应该是16位。我读过其他一些帖子,上面说android是32位的,但是官方NDK提供的wchar.h说wchar\u 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. 5.2/wchar\u t支持: - - - - - - - - - - - 正如文献记载的那样,Android平台直到2008年才真正支持wchar\t 安卓2.3。这实际上意味着: -如果您的目标平台是android-9或更高版本,则wchar\u t的大小为 C库中提供了4个字节和最宽的字符函数 (多字节编码/解码功能和 wsprintf/wsscanf)。 -如果您以任何先前的API级别为目标,则wchar\u t的大小将为1字节 无论如何,宽字符函数都不能工作。 我们建议所有开发人员摆脱对wchar\t类型的依赖 并切换到更好的表示。Android中提供的支持仅限于 这里提供了帮助您迁移现有代码的工具。 因为你的目标是安卓1.6,看起来wchar____t不适合你 即使在Android 2.3平台(“Android-9”)中,在许多地方仍然有注释,包括
wchar.h
,这意味着wchar\u t
是一个字节,没有实现宽字符库函数。这表明该实现可能仍然是不可靠的,因此我会非常谨慎地在任何Android版本上使用wchar\u t
如果您正在寻找替代方案,我发现这是一个优秀且非常轻量级的库。这有点旧,但我在搜索解决方案时遇到了这个问题。
NDK(我的r8d)似乎仍然不支持wsprintf: 看见 和。
在我的例子中,我正在使用libjson(考虑切换到yajl)来实现iOS/Android共享本机代码。
在切换库之前,我的NDK解决方案是:
double value = 0.5; // for example
std::wstringstream wss;
wss << value;
return json_string(wss.str());
double value=0.5;//例如
std::wstringstream wss;
wss顺便说一句,我的目标是android 1.6及以上版本检查这个问题:已经尝试过了,fshort wchar对我不起作用,不知道:(我使用sizeof在android上测试了wchar的大小,它是1…我找不到UTF8-CPP的许可细节。你知道它是否有任何限制吗?