I'm trying to turn a C++ std::string into a jstring and return it. This would be easy enough with
JNIEnv*->NewStringUTF(stdString.c_str())
but the problem is that the string I'm converting has almost randomly interspersed null characters in it. This is a problem for the c_str()
, but not the std::string
. NewStringUTF
will only catch a portion of the full std::string
. There is some hope in that the std::string
has a length()
function, which gets the full length, ignoring the problematic char* \0
characters.
There's a separate function NewString that takes in a jchar* and a jsize*, so that looks promising, but I can't get the std::string converted properly to the jchar*. I tried making it a byte array, but I probably wasn't doing it right. I had further problems converting the int given by the length()
into a jsize, which was required by the NewString call.
I have done a bit of work with vector<char> byteArray(stdString.begin(), stdString.end())
, but that didn't get me very far, probably because that is messing up what the original string is.
Here is the basic starter function I have, which worked with strings without null chars:
jstring StringToJString(JNIEnv * env, const std::string & nativeString) {
return env->NewStringUTF(nativeString.c_str());
}
As a side note, this function is being used inside a JNI wrapper file for returning an object's std::string
.
Thanks for any help or information sources!
vector<char>
in C++ andjbyteArray
on the Java side. No strings. UsingNewStringUTF
is especially bad, since the JVM will transform your buffer to UTF, thinking you are passing it a string.