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 oldsize = env->GetArrayLength(array);
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
jstring rtn=0;
5
int slen = (int)strlen(str_tmp);
6
unsigned short* buffer=0;
7
if(slen == 0)
8
{
9
rtn = env->NewStringUTF(str_tmp);
10
}
11
else
12
{
13
int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, NULL, 0);
14
buffer = (unsigned short*)malloc(length*2+1);
15
if(MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, (LPWSTR)buffer, length) > 0)
16
{
17
rtn = env->NewString((jchar*)buffer, length);
18
}
19
}
20
if(buffer)
21
{
22
free(buffer);
23
}
24
return rtn;
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
jbyteArray firstMacArray = env->NewByteArray( 6 );
5
......
6
jbyte *bytes = env->GetByteArrayElements( firstMacArray, 0);
7
for ( int i = 0; i < sizeof( pAdapterInfo->Address ); i++ )
8
{
9
bytes[ i ] = pAdapterInfo->Address[ i ];
10
}
11
12
env->SetByteArrayRegion(firstMacArray, 0, 6, bytes );
13
return firstMacArray;
14
}
1
//jstring to char*
2
char* jstringTostring(JNIEnv* env, jstring jstr)
3
{
4
char* rtn = NULL;
5
jclass clsstring = env->FindClass("java/lang/String");
6
jstring strencode = env->NewStringUTF("utf-8");
7
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
8
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
9
jsize alen = env->GetArrayLength(barr);
10
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
11
if (alen > 0)
12
{
13
rtn = (char*)malloc(alen + 1);
14
memcpy(rtn, ba, alen);
15
rtn[alen] = 0;
16
}
17
env->ReleaseByteArrayElements(barr, ba, 0);
18
return rtn;
19
}
1
char* to jstring
2
jstring stoJstring(JNIEnv* env, const char* pat)
3
{
4
jclass strClass = env->FindClass("Ljava/lang/String;");
5
jmethodID ctorID = env->GetMethodID(strClass, "", "([BLjava/lang/String;)V");
6
jbyteArray bytes = env->NewByteArray(strlen(pat));
7
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
8
jstring encoding = env->NewStringUTF("utf-8");
9
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
10
}
1
//将jstring类型转换成windows类型
2
char* jstringToWindows( JNIEnv *env, jstring jstr )
3
{
4
int length = (env)->GetStringLength(jstr );
5
const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
6
char* rtn = (char*)malloc( length*2+1 );
7
int size = 0;
8
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
9
if( size <= 0 )
10
return NULL;
11
(env)->ReleaseStringChars(jstr, jcstr );
12
rtn[size] = 0;
13
return rtn;
14
}
1
//将windows类型转换成jstring类型
2
jstring WindowsTojstring( JNIEnv* env, char* str )
3
{
4
jstring rtn = 0;
5
int slen = strlen(str);
6
unsigned short * buffer = 0;
7
if( slen == 0 )
8
rtn = (env)->NewStringUTF(str );
9
else
10
{
11
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
12
buffer = (unsigned short *)malloc( length*2 + 1 );
13
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
14
rtn = (env)->NewString( (jchar*)buffer, length );
15
}
16
if( buffer )
17
free( buffer );
18
return rtn;
19
}
20
/*JNIEXPORT jstring JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_strcal
21
(JNIEnv *env, jclass obj, jstring jstr1, jstring jstr2)
22
{
23
jbyteArray bytes = 0;
24
jthrowable exc;
25
char *pszResult = NULL;
26
char *pszSTR1 = NULL;
27
char *pszSTR2 = NULL;
28
29
pszSTR1 = jstringTostring(env, jstr1);
30
pszSTR2 = jstringTostring(env, jstr2);
33
int nlen = sizeof(char)*(strlen(pszSTR1)+strlen(pszSTR2));
34
pszResult = (char*)malloc(nlen);
35
strcpy(pszResult, pszSTR1);
36
strcat(pszResult, pszSTR2);
38
jstring jstrRe = stoJstring(env, pszResult);
39
free(pszSTR1);
40
free(pszSTR2);
41
free(pszResult);
42
return(jstrRe);
}
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(pDataIn, arrayBody, 0);