<p>Let's say that on the C++ side my function takes a variable of type <code>jstring</code> named <code>myString</code>. I can convert it to an ANSI string as follows:</p>

<pre><code>const char* ansiString = env-&gt;GetStringUTFChars(myString, 0);
</code></pre>

<p>is there a way of getting</p>

<p><code>const wchar_t* unicodeString =</code> ...</p>

[{'answer_id': 68065, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 2, 'selected': False, 'text': '<p>A portable and robust solution is to use <a href="http://www.gnu.org/software/libiconv/" rel="nofollow noreferrer">iconv</a>, with the understanding that you have to know what encoding your system <code>wchar_t</code> uses (UTF-16 on Windows, UTF-32 on many Unix systems, for example).</p>\n\n<p>If you want to minimise your dependency on third-party code, you can also hand-roll your own UTF-8 converter. This is easy if converting to UTF-32, somewhat harder with UTF-16 because you have to handle surrogate pairs too. :-P Also, you must be careful to reject <a href="http://www.google.com/search?q=utf-8+non-shortest" rel="nofollow noreferrer">non-shortest</a> forms, or it can open up security bugs in some cases.</p>\n'}, {'answer_id': 68066, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 1, 'selected': False, 'text': '<p>If we are not interested in cross platform-ability, in windows you can use the MultiByteToWideChar function, or the helpful macros A2W (ref. <a href="http://weblogs.asp.net/kennykerr/archive/2008/07/24/visual-c-in-short-converting-between-unicode-and-utf-8.aspx" rel="nofollow noreferrer">example</a>).</p>\n'}, {'answer_id': 68754, 'author': 'Adam Mitz', 'author_id': 2574, 'author_profile': 'https://Stackoverflow.com/users/2574', 'pm_score': 2, 'selected': False, 'text': "<p>JNI has a GetStringChars() function as well.  The return type is const jchar*, jchar is 16-bit on win32 so in a way that would be compatible with wchar_t.  Not sure if it's real UTF-16 or something else...</p>\n"}, {'answer_id': 804654, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Just use env->GetStringChars(myString, 0);\nJava pass Unicode by it's nature</p>\n"}, {'answer_id': 1666532, 'author': 'Benj', 'author_id': 193128, 'author_profile': 'https://Stackoverflow.com/users/193128', 'pm_score': 2, 'selected': False, 'text': "<p>I know this was asked a year ago, but I don't like the other answers so I'm going to answer anyway.  Here's how we do it in our source:</p>\n\n<pre><code>wchar_t * JavaToWSZ(JNIEnv* env, jstring string)\n{\n    if (string == NULL)\n        return NULL;\n    int len = env-&gt;GetStringLength(string);\n    const jchar* raw = env-&gt;GetStringChars(string, NULL);\n    if (raw == NULL)\n        return NULL;\n\n    wchar_t* wsz = new wchar_t[len+1];\n    memcpy(wsz, raw, len*2);\n    wsz[len] = 0;\n\n    env-&gt;ReleaseStringChars(string, raw);\n\n    return wsz;\n}\n</code></pre>\n\n<p><strong>EDIT</strong>: This solution works well on platforms where wchar_t is 2 bytes, some platforms have a 4 byte wchar_t in which case this solution will not work.</p>\n"}, {'answer_id': 2295676, 'author': 'Andreas Rieder', 'author_id': 276886, 'author_profile': 'https://Stackoverflow.com/users/276886', 'pm_score': 2, 'selected': False, 'text': '<p>And who frees wsz?\nI would recommend STL!</p>\n\n<pre><code>std::wstring JavaToWSZ(JNIEnv* env, jstring string)\n{\n    std::wstring value;\n    if (string == NULL) {\n        return value; // empty string\n    }\n    const jchar* raw = env-&gt;GetStringChars(string, NULL);\n    if (raw != NULL) {\n        jsize len = env-&gt;GetStringLength(string);\n        value.assign(raw, len);\n        env-&gt;ReleaseStringChars(string, raw);\n    }\n    return value;\n}\n</code></pre>\n'}, {'answer_id': 4154448, 'author': 'Vladimir Ivanov', 'author_id': 473070, 'author_profile': 'https://Stackoverflow.com/users/473070', 'pm_score': 0, 'selected': False, 'text': '<p>Rather simple. But do not forget to free the memory by ReleaseStringChars</p>\n\n<pre><code>JNIEXPORT jboolean JNICALL Java_TestClass_test(JNIEnv * env, jobject, jstring string)\n{\n    const wchar_t * utf16 = (wchar_t *)env-&gt;GetStringChars(string, NULL);\n    ...\n    env-&gt;ReleaseStringChars(string, utf16);\n}\n</code></pre>\n'}, {'answer_id': 9100041, 'author': 'gergonzalez', 'author_id': 973036, 'author_profile': 'https://Stackoverflow.com/users/973036', 'pm_score': 4, 'selected': False, 'text': "<p>If this helps someone... I've used this function for an Android project:</p>\n\n<pre><code>std::wstring Java_To_WStr(JNIEnv *env, jstring string)\n{\n    std::wstring value;\n\n    const jchar *raw = env-&gt;GetStringChars(string, 0);\n    jsize len = env-&gt;GetStringLength(string);\n    const jchar *temp = raw;\n    while (len &gt; 0)\n    {\n        value += *(temp++);\n        len--;\n    }\n    env-&gt;ReleaseStringChars(string, raw);\n\n    return value;\n}\n</code></pre>\n\n<p>An improved solution could be (Thanks for the feedback):</p>\n\n<pre><code>std::wstring Java_To_WStr(JNIEnv *env, jstring string)\n{\n    std::wstring value;\n\n    const jchar *raw = env-&gt;GetStringChars(string, 0);\n    jsize len = env-&gt;GetStringLength(string);\n\n    value.assign(raw, raw + len);\n\n    env-&gt;ReleaseStringChars(string, raw);\n\n    return value;\n}\n</code></pre>\n"}, {'answer_id': 53702206, 'author': 'shizhen wang', 'author_id': 10769816, 'author_profile': 'https://Stackoverflow.com/users/10769816', 'pm_score': 0, 'selected': False, 'text': '<p>I try to jstring->char->wchar_t</p>\n\n<pre><code>char* js2c(JNIEnv* env, jstring jstr)\n{\n    char* rtn = NULL;\n    jclass clsstring = env-&gt;FindClass("java/lang/String");\n    jstring strencode = env-&gt;NewStringUTF("utf-8");\n    jmethodID mid = env-&gt;GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");\n    jbyteArray barr = (jbyteArray)env-&gt;CallObjectMethod(jstr, mid, strencode);\n    jsize alen = env-&gt;GetArrayLength(barr);\n    jbyte* ba = env-&gt;GetByteArrayElements(barr, JNI_FALSE);\n    if (alen &gt; 0)\n    {\n        rtn = (char*)malloc(alen + 1);\n        memcpy(rtn, ba, alen);\n        rtn[alen] = 0;\n    }\n    env-&gt;ReleaseByteArrayElements(barr, ba, 0);\n    return rtn;\n}\n\njstring c2js(JNIEnv* env, const char* str) {\n    jstring rtn = 0;\n    int slen = strlen(str);\n    unsigned short * buffer = 0;\n    if (slen == 0)\n        rtn = (env)-&gt;NewStringUTF(str);\n    else {\n        int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0);\n        buffer = (unsigned short *)malloc(length * 2 + 1);\n        if (MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) &gt; 0)\n            rtn = (env)-&gt;NewString((jchar*)buffer, length);\n        free(buffer);\n    }\n    return rtn;\n}\n\n\n\njstring w2js(JNIEnv *env, wchar_t *src)\n{\n    size_t len = wcslen(src) + 1;\n    size_t converted = 0;\n    char *dest;\n    dest = (char*)malloc(len * sizeof(char));\n    wcstombs_s(&amp;converted, dest, len, src, _TRUNCATE);\n\n    jstring dst = c2js(env, dest);\n    return dst;\n}\n\nwchar_t *js2w(JNIEnv *env, jstring src) {\n\n    char *dest = js2c(env, src);\n    size_t len = strlen(dest) + 1;\n    size_t converted = 0;\n    wchar_t *dst;\n    dst = (wchar_t*)malloc(len * sizeof(wchar_t));\n    mbstowcs_s(&amp;converted, dst, len, dest, _TRUNCATE);\n    return dst;\n}\n</code></pre>\n'}, {'answer_id': 57033639, 'author': 'Eng.Fouad', 'author_id': 597657, 'author_profile': 'https://Stackoverflow.com/users/597657', 'pm_score': 0, 'selected': False, 'text': '<p>Here is how I converted <code>jstring</code> to <code>LPWSTR</code>.</p>\n\n<pre><code>const char* nativeString = env-&gt;GetStringUTFChars(javaString, 0);\nsize_t size = strlen(nativeString) + 1;\nLPWSTR lpwstr = new wchar_t[size];\nsize_t outSize;\nmbstowcs_s(&amp;outSize, lpwstr, size, nativeString, size - 1);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68042', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']