How can I convert char* to jcharArray in JNI?
What code do I have to put in // TODO comment part?
JNIEXPORT jcharArray JNICALL Java_finger_FingerPrintJNI_GetVer(JNIEnv *env, jobject thisObj){
char* version = getText(); // Returns char* data type
int version_len = strlen(version);
jcharArray j_version_array = env->NewCharArray(version_len + 1);
// TODO Convert char* to jcharArray
return jcharArray;
}
Add ======================================================================
As Gearon suggested, when I put the below code an error is raised.
jchar* jVersion = (jchar*)version;
env->SetCharArrayRegion(jcharArray, 0, version_len, jVersion);
Following is the error message.
JNIEnv_::SetCharArrayRegion(jcharArray,jsize,jsize,const jchar *)': cannot convert argument 1 from 'jchar *' to 'jcharArray'
sample.cpp(26): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Thanks for the code Gearon suggested my final code is something like this.
jchar* j_version = (jchar*)calloc(sizeof(jchar), version_len);
for(int i=0; i <= version_len; i++){
j_version[i] = (jchar) version[i];
}
jcharArray j_version_array = env->NewCharArray(version_len + 1);
env->SetCharArrayRegion(j_version_array, 0, version_len , j_version);
You can use
jchar* jVersion = (jchar*)version;
env->SetCharArrayRegion(j_version_array, 0, version_len, jVersion);
Hope it can help you. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With