Jni Converting Jstring to Char *

jni converting jstring to char

The pointer returned by GetStringUTFChars() is not valid after ReleaseStringUTFChars() is called. It sounds like the implementation happens to be re-using the storage for all three -- I would guess the pointer values are all the same.

Either use the values, or copy the strings with strdup(), before releasing them.

JNI converting jstring to char *

Here's a a couple of useful link that I found when I started with JNI

http://en.wikipedia.org/wiki/Java_Native_Interface

http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html

concerning your problem you can use this

JNIEXPORT void JNICALL Java_ClassName_MethodName(JNIEnv *env, jobject obj, jstring javaString)   
{
const char *nativeString = env->GetStringUTFChars(javaString, 0);

// use your string

env->ReleaseStringUTFChars(javaString, nativeString);
}

JNI call convert jstring to char*

According to the documentation,

GetStringUTFChars
const jbyte* GetStringUTFChars(JNIEnv *env, jstring string,
jboolean *isCopy);

Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by ReleaseStringUTFChars().

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.

So the last parameter should be a jboolean;

jstring to char* in c usage not converting into printable format

The use of __atoe() function worked for me in the case.

Java - JNI - Casting UCS-2 character to jchar (i.e Java's char)

jchar is define in jhi.h as:

typedef unsigned short     jchar;

If you are trying to return a string to java, take a look ant the JNI function NewStringUTF() which takes a C string and returns a java string

NewStringUTF

Android JNI - Reliable way to convert jstring to wchar_t

As long as all your data is UCS2, you can use option 1. Please see wchar_t for UTF-16 on Linux? for a similar discussion. Note that C++11 provides std::codecvt_utf16 to deal with the situation.

Convert jbytearray to char* with wrong code in JNI

If you're going to use new char[] to create string data, and then use that data in functions that require null-termination, you have to explicitly add the null terminator.

In addition, you may not have allocated enough space for the terminating null, thus you need to allocate len + 1 bytes.

You can do this:

char* buff = new char[len + 1]();

which fills the buffer with null characters or:

char* buff = new char[len + 1];
buff[len] = '\0';

However I highly suggest you use a std::string or std::vector<char> instead of doing things this way.

Since JNI is very fragile, if an exception is thrown, you may get memory leaks using raw pointers this way.

Here is a small routine you can use to convert from a Java byte array to a vector:

#include <vector>
//...
jbyteArray jba = (jbyteArray) env->CallStaticObjectMethod(clazz, methodId, dataPath);
int len = env->GetArrayLength (jba);
std::vector<char> buff(len + 1, 0);
env->GetByteArrayRegion (jba, 0, len, reinterpret_cast<jbyte*>(buff.data()));
Log_d(LOG_TAG, "getPkgData: %s", buff.data());

This will not suffer from memory leaks if an exception is thrown, since std::vector will automatically deallocate the memory when the vector goes out of scope for any reason.



Related Topics



Leave a reply



Submit