How to convert char* to jcharArray in JNI

 ̄綄美尐妖づ 提交于 2021-01-27 06:53:00

问题


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

回答1:


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);



回答2:


You can use

jchar* jVersion = (jchar*)version;
env->SetCharArrayRegion(j_version_array, 0, version_len, jVersion);

Hope it can help you. Thanks.



来源:https://stackoverflow.com/questions/38493156/how-to-convert-char-to-jchararray-in-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!