三.NDK类型转换

NDK的基本类型转换,是NDK使用的基础,本文先讲基本的数据类型.一定程度上,基本数据类型可以直接强转

一.Java to C Convert

先看看Java2CConvert.h,实现的功能有三个

  • jint 转 c int
  • jlong 转 c long
  • jstring 转 c const char *
#ifndef PRACTICLE_JAVACONVERTC_H
#define PRACTICLE_JAVACONVERTC_H

#include <jni.h>

class Java2CConvert {
public:
    int convertInt(JNIEnv *env, jint srcInt);

    long convertLong(JNIEnv *env, jlong srcLong);

    const char *convertString(JNIEnv *env, jstring srcString);
};

#endif //PRACTICLE_JAVACONVERTC_H

Java2CConvert.cpp实现如下

#include "include/Java2CConvert.h"
int Java2CConvert::convertInt(JNIEnv *env, jint srcInt) {
    return srcInt;
};

long Java2CConvert::convertLong(JNIEnv *env, jlong srcLong) {
    return srcLong;
};

const  char *Java2CConvert::convertString(JNIEnv *env, jstring srcString) {
    const char *nativeString = env->GetStringUTFChars(srcString, JNI_FALSE);
    return nativeString;
};

二.C to Java

C2JavaConvert的主要功能

  • c int 转 jint
  • c long 转 jlong
  • c const char * 转 jstring
//防止重复编译的头
#ifndef PRACTICLE_HELLO_H
#define PRACTICLE_HELLO_H

#include <android/log.h>
#include <jni.h>


class C2JavaConvert {
public:

    jint convertInt(JNIEnv *env, int srcInt);
    jlong convertLong(JNIEnv *env, long srcLong);
    jstring convertString(JNIEnv *env, const char* srcString);
};

//防止重复编译的头结束标识
#endif //PRACTICLE_HELLO_H

C2JavaConvert.cpp

#include "C2JavaConvert.h"


jint C2JavaConvert::convertInt(JNIEnv *env, int srcInt) {
    return srcInt;
};

jlong C2JavaConvert::convertLong(JNIEnv *env, long srcLong) {
    return srcLong;
};

jstring C2JavaConvert::convertString(JNIEnv *env, const char *srcString) {
    return env->NewStringUTF(srcString);
};

三.调用

hello.cpp负责供JNI来调用

#include <C2JavaConvert.h>
#include "Hello.h"
#include <Java2CConvert.h>


#ifdef __cplusplus
extern "C" {
#endif


JNIEXPORT jint JNICALL
Java_com_example_a3_1cmake_MainActivity_convertInt(JNIEnv *env, jobject instance, jint src) {
    Java2CConvert *java2CConvert = new Java2CConvert();
    C2JavaConvert *c2JavaConvert = new C2JavaConvert();
    int cInt = java2CConvert->convertInt(env, src);
    return c2JavaConvert->convertInt(env, cInt);
}


JNIEXPORT jlong JNICALL
Java_com_example_a3_1cmake_MainActivity_convertLong(JNIEnv *env, jobject instance, jlong src) {
    Java2CConvert *java2CConvert = new Java2CConvert();
    C2JavaConvert *c2JavaConvert = new C2JavaConvert();
    long cInt = java2CConvert->convertLong(env, src);
    return c2JavaConvert->convertLong(env, cInt);
}


JNIEXPORT jstring JNICALL
Java_com_example_a3_1cmake_MainActivity_convertString(JNIEnv *env, jobject instance, jstring src) {
    Java2CConvert *java2CConvert = new Java2CConvert();
    C2JavaConvert *c2JavaConvert = new C2JavaConvert();
    const char *chars = java2CConvert->convertString(env, src);
    return c2JavaConvert->convertString(env, chars);
}


#ifdef __cplusplus
}
#endif

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView contentTv = findViewById(R.id.tv_content);
        contentTv.setText("int = " + convertInt(10) + "\t long = " + convertLong(11L) + "  string = " + convertString("hello world"));
    }
    static {
        //加载静态库
        System.loadLibrary("native-lib");
    }
    public native int convertInt(int src);
    public native long convertLong(long src);
    public native String convertString(String srcString);

}

在这里插入图片描述

code:https://github.com/HumorSmith/NDKPraticle/tree/master/3_datatype_convert

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页