JNI各类型参数互相调用示例(二)
(2013-01-09 21:40:58)
标签:
ndkjniandroidit |
jni和java之间字符串的转换方法。
C的实现:
JNIEXPORT
jstring JNICALL Java_Android123_CwjC (JNIEnv *env, jobject obj,
jstring string)
{
char
szBuffer[255];
return
(*env)->NewStringUTF(env, szBuffer);
}
C++的实现:
JNIEXPORT
jstring JNICALL Java_Android123_CwjCpp (JNIEnv *env, jobject obj,
jstring string)
{
const
char *strUTF = env->GetStringUTFChars(string,
0);
char
szBuffer[255];
strcpy(szBuffer,
strUTF);
env->ReleaseStringUTFChars(string,
strUTF);
return
env->NewStringUTF(szBuffer);
}
java中string与byte[]的转换
1.string 转 byte[]
byte[] midbytes=isoString.getBytes("UTF8");
//为UTF8编码
byte[] isoret = srt2.getBytes("ISO-8859-1");
//为ISO-8859-1编码
其中ISO-8859-1为单字节的编码
2.byte[]转string
String isoString = new String(bytes,"ISO-8859-1");
String srt2=new String(midbytes,"UTF-8");
|
//jbytearray转c++byte数组 |
||
|
jbyte * arrayBody = env->GetByteArrayElements(data,0); |
||
|
3 |
jsize theArrayLengthJ = env->GetArrayLength(data); |
|
|
4 |
BYTE * starter = (BYTE *)arrayBody; |
|
|
1 |
//jbyteArray 转 c++中的BYTE[] |
|
|
2 |
//jbytearray array |
|
|
3 |
jbyte * olddata = (jbyte*)env->GetByteArrayElements(array, 0); |
|
|
4 |
jsize |
|
|
5 |
BYTE* bytearr = (BYTE*)olddata; |
|
|
6 |
int len = (int)oldsize; |
|
|
1 |
//C++中的BYTE[]转jbyteArray |
|
|
2 |
//nOutSize是BYTE数组的长度 BYTE pData[] |
|
|
3 |
jbyte *by = (jbyte*)pData; |
|
|
4 |
jbyteArray jarray = env->NewByteArray(nOutSize); |
|
|
5 |
env->SetByteArrayRegin(jarray, 0, nOutSize, by); |
|
1 |
//jbyteArray 转 char * |
|
|
2 |
char* data = (char*)env->GetByteArrayElements(strIn, 0); |
|
|
1 |
//char* 转jstring |
|
|
2 |
jstring WindowsTojstring(JNIEnv* env, char* str_tmp) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
||
|
10 |
|
||
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
} |
|
1 |
//jstring 转 char* 或者 const char* |
|
|
2 |
// jstring str |
|
|
3 |
const char *key = env->GetStringUTFChars(str, 0); |
|
|
4 |
//jboolean isOffer |
|
|
5 |
jsClient->modify(key, isOffer); |
|
|
6 |
//需要释放,否则容易内存泄露 |
|
|
7 |
env->ReleaseStringUTFChars(str, key); |
|
1 |
//JNI 返回 jbyteArray |
|
|
2 |
JNIEXPORT jbyteArray JNICALL Java_Test_getByteArray(JNIEnv *env, jobject obj) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
||
|
10 |
|
||
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|||
|
14 |
} |
|||
|
1 |
//jstring to char* |
|||
|
2 |
char* jstringTostring(JNIEnv* env, jstring jstr) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
} |
|
|
1 |
////char* to jstring |
|
|
2 |
jstring stoJstring(JNIEnv* env, const char* pat) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
||
|
10 |
} |
||
|
1 |
//将jstring类型转换成windows类型 |
|
|
2 |
char* jstringToWindows( JNIEnv *env, jstring jstr ) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
||
|
10 |
|
||
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
} |
|
|
1 |
//将windows类型转换成jstring类型 |
|
|
2 |
jstring WindowsTojstring( JNIEnv* env, char* str ) |
|
|
3 |
{ |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
||
|
10 |
|
||
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
14 |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
} |
|
|
20 |
|
|
|
1 |
//从传进来的对象中取出byte[] |
|
|
2 |
jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B"); |
|
|
3 |
jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData); |
|
|
4 |
jsize theArrayLeng = env->GetArrayLength(pDataIn); |
|
|
1 |
//byte[]转为BYTE[] |
|
|
2 |
jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0); |
|
|
3 |
BYTE * jDataIn = (BYTE *)arrayBody; |
|
1 |
//将BYTE数组转为jarray |
|
|
2 |
jbyte* byte =
(jbyte*)jDataOut; |
|
|
3 |
jbyteArray jarray = env->NewByteArray(theArrayLeng); |
|
|
4 |
env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte); |
|
|
1 |
//给每一个实例的变量付值 |
|
|
2 |
(env)->SetObjectField(objDataIn,byteData,jarray); |
|
|
3 |
(env)->SetIntField(objDataIn,pDataInLen,jDataInLen); |
|
|
4 |
(env)->ReleaseByteArrayElements |
|

加载中…