美文网首页
JNI java中String,byte[]与c中char*转换

JNI java中String,byte[]与c中char*转换

作者: yu_yue | 来源:发表于2018-05-15 18:29 被阅读10次

    JNIEXPORT jbyteArray JNICALL
    Java_com_nodepp_smartnode_utils_Utils_encrypt(JNIEnv env, jobject instance, jbyteArray data_,jbyteArray key_) {
    // LOGD("--------encrypt--------------");
    char *data = (char ) (env)->GetByteArrayElements(env, data
    , NULL);//string转char*
    char key = (char ) (env)->GetByteArrayElements(env, key_, NULL);
    int in_len = (
    env)->GetArrayLength(env, data_); // byte数组的长度
    int out_len = in_len+32;
    char data_out = (char )malloc(out_len sizeof(char));
    int encryp_code = xxtea_encrypt(data, in_len, key, data_out, &out_len);
    jbyteArray result = (
    env)->NewByteArray(env, out_len);
    //char
    转java的byte[]
    (*env)->SetByteArrayRegion(env, result, 0, out_len, (jbyte ) data_out);
    (
    env)->ReleaseByteArrayElements(env, data_, data, 0);
    free(data_out);//记得释放内存
    return result;
    }

    相关文章

      网友评论

          本文标题:JNI java中String,byte[]与c中char*转换

          本文链接:https://www.haomeiwen.com/subject/czyydftx.html