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

2018-05-15  本文已影响10人  yu_yue

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

上一篇下一篇

猜你喜欢

热点阅读