Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from basic_string to jstring

I'm using a basic_string<wchar_t> type and need to convert it into a jstring to pass through a JNI layer. I'm wondering what the best way to do that is. I have a function that can give me a std::string from my basic_string<wchar_t> type, so an answer to that would also be cool.

Cheers.

like image 439
sparkFinder Avatar asked Aug 08 '11 23:08

sparkFinder


1 Answers

You'll need to convert the std::basic_string into UTF-8. Look into what your wstring -> string conversion does.

Sun has a JNI tutorial that shows how to convert a char* into a jstring (using some UTF conversion routines). You could use your wstring->string, then pass in string.c_str() to the NewStringUTF function:

Untested Code:

JNIEXPORT jstring JNICALL StringTest(JNIEnv *env) {
    const char* test = "something";
    return env->NewStringUTF(test);
}
like image 124
Tim Finer Avatar answered Sep 30 '22 19:09

Tim Finer