12

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!

4
  • why does your string have null terminating characters in the middle of it? It sounds like you are using the wrong container for your data.
    – Red Alert
    Dec 4, 2014 at 20:51
  • We're essentially turning an array of doubles into a string, so sections of the double bytes are the same as the null char. I can't just print out the doubles and put it as an ascii string, thet's way too slow, and has whitespace and other not-necessary things.
    – Eli Rising
    Dec 4, 2014 at 20:54
  • 3
    Strings, both in C++ and Java, are meant to store things relevant to natural language. If you need to move doubles to a buffer of some sort, you should be using a vector<char> in C++ and jbyteArray on the Java side. No strings. Using NewStringUTF is especially bad, since the JVM will transform your buffer to UTF, thinking you are passing it a string.
    – Red Alert
    Dec 4, 2014 at 20:57
  • 1
    @RedAlert Actually, NewStringUTF is for modified UTF-8, which isn't a Unicode Consortium format. Dec 6, 2014 at 0:11

1 Answer 1

4

I made my own workaround solution taking RedAlert's advice.

Java now expects a byte[] from the native call:

private native byte[] toString(long thiz);

The toString method now calls this method inside it on a std::string:

jbyteArray StringToJByteArray(JNIEnv * env, const std::string &nativeString) {
    jbyteArray arr = env->NewByteArray(nativeString.length());
    env->SetByteArrayRegion(arr,0,nativeString.length(),(jbyte*)nativeString.c_str());
    return arr;
}

And the java level receives the data like this:

byte[] byteArray = toString(mNativeInstance);
String nativeString = "";
try {
    nativeString = new String(byteArray, "UTF-8");
} catch (UnsupportedEncodingException e) {
    Log.e(TAG, "Couldn't convert the jbyteArray to UTF-8");
    e.printStackTrace();
}
7
  • 2
    Why are you still using strings? It doesn't many any sense to encode an array of doubles using UTF-8
    – Red Alert
    Dec 5, 2014 at 3:37
  • I was trying to store the data in a database, and our previous method used strings. I was serializing the data previously into base64, but that was really slow, so now I'm just taking the raw byte data and moving that around. The original question was about strings as well, so I'm keeping the answer relevant to the question.
    – Eli Rising
    Dec 5, 2014 at 18:04
  • You database can't store binary data?
    – Red Alert
    Dec 5, 2014 at 21:45
  • An arbitrary sequence of byte values cannot be interpreted as UTF-8. If you absolutely must convert bytes to strings, use CP437 because it allows any sequence of byte values 0-255 and every character they represent also exists in Unicode. (Others, like, Windows-1252, have fewer than 256 characters.) Then, having Unicode strings, you can store them in NCHAR/NVARCHAR fields. But, as @RedAlert suggests, binary data is binary data. Dec 6, 2014 at 0:11
  • 1
    This post lead to the solution that fixed a crash in Android 5. Many of the keyboard emoji will crash the jni NewStringUTF JNI call. You have to push this native string over as a byte array and properly encode it on the java side and hand it back as a jstring to get around it. Massive help. Honestly I spent more days than I care to admit on this. I think its only a problem if your trying to parse a lot of variable user input. but it raised our crash rate alarmingly as Android 5.x is on the uptake. and its not a crash on <5.x by many of the characters are malformed. Dec 12, 2015 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.