简体   繁体   English

从std :: wstring转换为jstring

[英]convert to jstring from std::wstring

I'm trying to convert a wstring from my cpp code to jstring. 我正在尝试将wstring从我的cpp代码转换为jstring。

I know there's a method named NewStringUTF to get jstring from std::string (after getting the const char* from that string of course). 我知道有一个名为NewStringUTF的方法,可从std :: string获取jstring(当然是从该字符串获取const char *之后)。

is there a way I can do the same if the input is std::wstring without using iconv? 如果输入是std :: wstring而不使用iconv,有什么办法可以做同样的事情吗?

JNI has NewString(JNIEnv * , const jchar * , jsize) function. JNI具有NewString(JNIEnv *,const jchar *,jsize)函数。 If your std::wstring::c_str() returns a pointer compatible with jchar* (this depends on your platform), you can try this function. 如果您的std::wstring::c_str()返回与jchar *兼容的指针(这取决于您的平台),则可以尝试使用此函数。 Note that if your wchar_t is 32 bit (as on Linux), you cannot use it directly. 请注意,如果您的wchar_t是32位(如在Linux上),则不能直接使用它。 See Android JNI - Reliable way to convert jstring to wchar_t and How do I convert jstring to wchar_t * for more info. 有关更多信息,请参见Android JNI-将jstring转换为wchar_t的可靠方法如何将jstring转换为wchar_t *

The easy form is to convert your std::wstring to std::string to use the NewStringUTF. 最简单的形式是将您的std :: wstring转换为std :: string以使用NewStringUTF。

Here you have a thread with the conversion How to convert wstring into string? 这里有一个转换线程如何将wstring转换成string?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM