c++基本数据转换工具
2022-11-24 22:39:44
michael007js
124
头文件
#ifndef ENTERTAINMENTFORMICHAELNDK_CONVERT_H #define ENTERTAINMENTFORMICHAELNDK_CONVERT_H #include "jni.h" #include "string" int string2int(std::string str); std::string int2String(int i); char *string2char(std::string str); std::string char2string(char *chars); const char *string2Char(std::string str); char *jstring2Char(JNIEnv *env, jstring jstr); jstring char2Jstring(JNIEnv *env, const char *pat); jstring str2jstring(JNIEnv *env1, std::string str); std::string jstring2str(JNIEnv *env, jstring jstr); jbyteArray char2jbyteArray(JNIEnv *env, char *data, int length); char *jbyteArray2Char(JNIEnv *env, jbyteArray bytearray); jbyteArray string2JArray(JNIEnv *env, std::string str); std::string jArray2string(JNIEnv *env, jbyteArray byteArray); #endif //ENTERTAINMENTFORMICHAELNDK_CONVERT_H
c文件
#include "convert.h" #include "jni.h" #include "string" #include "stdio.h" int string2int(std::string str) { int t_port = atoi(str.c_str()); return t_port; } std::string int2String(int i) { return std::to_string(i); } char *string2char(std::string str) { std::string s1 = str; char *data; int len = s1.length(); data = (char *) malloc((len + 1) * sizeof(char)); s1.copy(data, len, 0); return data; } const char *string2Char(std::string str) { return str.c_str(); } std::string char2string(char *chars) { std::string s = chars; return s; } char *jstring2Char(JNIEnv *env, jstring jstr) { char *rtn = NULL; jclass clsstring = env->FindClass("java/lang/String"); jstring strencode = env->NewStringUTF("utf-8"); jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode); jsize alen = env->GetArrayLength(barr); jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE); if (alen > 0) { rtn = (char *) malloc(alen + 1); memcpy(rtn, ba, alen); rtn[alen] = 0; } env->ReleaseByteArrayElements(barr, ba, 0); return rtn; } jstring char2Jstring(JNIEnv *env, const char *pat) { jclass strClass = env->FindClass("java/lang/String"); jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); jbyteArray bytes = env->NewByteArray(strlen(pat)); env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte *) pat); jstring encoding = env->NewStringUTF("utf-8"); return (jstring) env->NewObject(strClass, ctorID, bytes, encoding); } jstring str2jstring(JNIEnv *env1, std::string str) { char *pat = string2char(str); jclass strClass = env1->FindClass("java/lang/String"); jmethodID ctorID = env1->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); jbyteArray bytes = env1->NewByteArray((jsize) strlen(pat)); env1->SetByteArrayRegion(bytes, 0, (jsize) strlen(pat), (jbyte *) pat); jstring encoding = env1->NewStringUTF("utf-8"); return (jstring) env1->NewObject(strClass, ctorID, bytes, encoding); } std::string jstring2str(JNIEnv *env, jstring jstr) { char *rtn = NULL; jclass clsstring = env->FindClass("java/lang/String"); jstring strencode = env->NewStringUTF("utf-8"); jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode); jsize alen = env->GetArrayLength(barr); jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE); if (alen > 0) { rtn = (char *) malloc(alen + 1); memcpy(rtn, ba, alen); rtn[alen] = 0; } env->ReleaseByteArrayElements(barr, ba, 0); std::string stemp(rtn); free(rtn); return stemp; } jbyteArray char2jbyteArray(JNIEnv *env, char *data, int length) { jbyte *by = (jbyte *) data; jbyteArray jarray = env->NewByteArray(length); env->SetByteArrayRegion(jarray, 0, length, by); return jarray; } char *jbyteArray2Char(JNIEnv *env, jbyteArray bytearray) { char *chars = NULL; jbyte *bytes; bytes = env->GetByteArrayElements(bytearray, 0); int chars_len = env->GetArrayLength(bytearray); chars = new char[chars_len + 1]; memset(chars, 0, chars_len + 1); memcpy(chars, bytes, chars_len); chars[chars_len] = 0; env->ReleaseByteArrayElements(bytearray, bytes, 0); return chars; } jbyteArray string2JArray(JNIEnv *env, std::string str) { char *pat = string2char(str); jbyteArray bytes = env->NewByteArray((jsize) strlen(pat)); return bytes; } std::string jArray2string(JNIEnv *env, jbyteArray byteArray) { char *chars = NULL; jbyte *bytes; bytes = env->GetByteArrayElements(byteArray, 0); int chars_len = env->GetArrayLength(byteArray); chars = new char[chars_len + 1]; memset(chars, 0, chars_len + 1); memcpy(chars, bytes, chars_len); chars[chars_len] = 0; env->ReleaseByteArrayElements(byteArray, bytes, 0); return char2string(chars); }