1

Good day

I have a question how to send my char array to jni c++ code? I know how send int number only..

A have this array char[] chars = password.toCharArray();

and private native void JNIEncrypt(char[] chars);

My JNI method look like this

Java_com_kru13_ndkdemos_MainActivity_JNIEncrypt( JNIEnv* env, jobject  obj, ?????)

I would like to ask how it should look JNI method? I need use char array in c++ code

than you

1

3 Answers 3

1

Why not just pass it as a String, which is jstring in JNI:

// Java side
private native void JNIEncrypt(String password);

// Native JNI side
Java_com_kru13_ndkdemos_MainActivity_JNIEncrypt(JNIEnv* env, jobject thiz, jstring password);

You can then access the string JNI side via GetStringChars() and related or GetStringUTFChars(). For example:

const char* utf8 = env->GetStringUTFChars(password, NULL);
// do something with utf8
env->ReleaseStringUTFChars(password, utf8);
Sign up to request clarification or add additional context in comments.

Comments

1

It would be:

Java_com_kru13_ndkdemos_MainActivity_JNIEncrypt( JNIEnv* env, jobject  obj, jcharArray array)

Inside the function you will have to use env->GetCharArrayElements(...).

Comments

0

toCharArray gets you the sequence of Unicode/UTF-16 code-units from the string. On Android, You will probably want Unicode/UTF-8 on the C++ side, but that depends on the C++ libraries you want to use. Once, you know, use the Java String class to get the bytes.

Note: GetStringUTFChars gives you modified UTF-8 code-units, which Unicode-compliant libraries reject for many characters.

private void JNIEncrypt(String string) {
    JNIEncrypt(string.getBytes("UTF-8")); 
    // Or, for the OS default, string.getBytes()
}

private native void JNIEncrypt(byte[] chars);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.