1. 程式人生 > >Android中JNI使用詳解(4)---Java與C之間資料型別轉換

Android中JNI使用詳解(4)---Java與C之間資料型別轉換

Jni中基本型別轉換對應的表格

Java型別

本地型別

說明

boolean

jboolean

無符號,8

byte

jbyte

無符號,8

char

jchar

無符號,16

short

jshort

有符號,16

int

jint

有符號,32

long

jlong

有符號,64

float

jfloat

32

double

jdouble

64

void

void


Java程式碼:

package com.example.jni;

public class NativeUtil {
// String 轉換
public static native String getJniString();
// int轉換
public static native int getJniInt();
// boolean轉換
public static native boolean getJniBoolean();
// double轉換
public static native double getJniDouble();
// float轉換
public static native float getJniFloat();
// Byte轉換
public static native float getJniByte();
// Byte陣列轉換
public static native byte[] getJniByteArray();
// String陣列轉換
public static native String[] getJniStringArray();
// int陣列轉換
public static native int[] getJniIntArray();
// Long陣列轉換
public static native long[] getJniLongArray();
}


C程式碼:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jni_NativeUtil */
#ifndef _Included_com_example_jni_NativeUtil
#define _Included_com_example_jni_NativeUtil
#define SEED_SIZE 20
#ifdef __cplusplus
extern "C" {
#endif
/*
 *  String 轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniString
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_jni_NativeUtil_getJniString(
JNIEnv *env, jclass jobj) {
//    GetStringUTFChars
return (*env)->NewStringUTF(env, "Hello JNI!");
}
/*
 * int 轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniInt
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_example_jni_NativeUtil_getJniInt(JNIEnv *env,
jclass jobj) {
int i = 13;
return (jint) i;
}
/*
 * boolean轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniBoolean
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_com_example_jni_NativeUtil_getJniBoolean(
JNIEnv *env, jclass jobj) {
unsigned char b = 0;
return (jboolean) b;
}
/*
 * double轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniDouble
 * Signature: ()D
 */
JNIEXPORT jdouble JNICALL Java_com_example_jni_NativeUtil_getJniDouble(
JNIEnv *env, jclass jobj) {
double d = 888.88;
return (jdouble) d;
}
/*
 * float轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniFloat
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_com_example_jni_NativeUtil_getJniFloat(
JNIEnv *env, jobject jobj) {
jbyte b = 5;
return (jbyte) b;
}
/*
 * byte轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniByte
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_com_example_jni_NativeUtil_getJniByte(JNIEnv *env,
jobject jobj) {
jfloat f = 5;
return (jfloat) f;
}
/*
 * byte陣列轉換
 * Class:     com_example_jni_NativeUtil
 * Method:    getJniByteArray
 * Signature: ()[B
 */
JNIEXPORT jbyteArray JNICALL Java_com_example_jni_NativeUtil_getJniByteArray(
JNIEnv *env, jobject jobj) {
int i = 0;
jbyteArray array = (*env)->NewByteArray(env, 16);
jbyte tmp_aeskey[16];
for (i = 0; i < 16; i++) {
jbyte b = 5;
tmp_aeskey[i] = b;
}
(*env)->SetByteArrayRegion(env, array, 0, 16, tmp_aeskey);
return array;
}
/**
 * int陣列轉換
 */
JNIEXPORT jintArray JNICALL Java_com_example_jni_NativeUtil_getJniIntArray(
JNIEnv *env, jobject jobj) {
int i = 1;
jintArray array; //定義陣列物件
array = (*env)->NewIntArray(env, 10);
for (; i <= 10; i++) {
(*env)->SetIntArrayRegion(env, array, i - 1, 1, &i);
}
jint* elems = (*env)->GetIntArrayElements(env, array, 0);
return array;
}
/**
 * long陣列轉換
 */
JNIEXPORT jlongArray JNICALL Java_com_example_jni_NativeUtil_getJniLongArray(
JNIEnv *env, jobject jobj) {
int i = 1;
jlongArray array; //定義陣列物件
array = (*env)->NewLongArray(env, 10);
for (; i <= 10; i++) {
(*env)->SetLongArrayRegion(env, array, i - 1, 1, &i);
}
jint* elems = (*env)->GetLongArrayElements(env, array, 0);
return array;
}
#ifdef __cplusplus
}
#endif
#endif