程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> JNI的某些數組和字符串類型轉換

JNI的某些數組和字符串類型轉換

編輯:C++入門知識

jbytearray轉c++byte數組
C代碼  [c]
 jbyte * arrayBody = env->GetByteArrayElements(data,0);  
jsize theArrayLengthJ = env->GetArrayLength(data);  
BYTE * starter = (BYTE *)arrayBody;  


jbyteArray 轉 c++中的BYTE[]
C代碼  [c]
 //jbytearray strIn 
jbyte * olddata = (jbyte*)env->GetByteArrayElements(strIn, 0); 
jsize  oldsize = env->GetArrayLength(strIn); 
BYTE* bytearr = (BYTE*)olddata; 
int len = (int)oldsize; 


C++中的BYTE[]轉jbyteArray
C代碼  [c]
//nOutSize是BYTE數組的長度 BYTE pData[] 
jbyte *by = (jbyte*)pData; 
jbyteArray jarray = env->NewByteArray(nOutSize); 
env->SetByteArrayRegin(jarray, 0, nOutSize, by); 


jbyteArray 轉 char *
C代碼  [c]
 char* data = (char*)env->GetByteArrayElements(strIn, 0); 

char* 轉jstring
C代碼  [c]
jstring WindowsTojstring(JNIEnv* env, char* str_tmp) 

 jstring rtn=0; 
 int slen = (int)strlen(str_tmp); 
 unsigned short* buffer=0; 
 if(slen == 0) 
 { 
  rtn = env->NewStringUTF(str_tmp); 
 } 
 else 
 { 
  int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, NULL, 0); 
  buffer = (unsigned short*)malloc(length*2+1); 
  if(MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, (LPWSTR)buffer, length) > 0) 
  { 
   rtn = env->NewString((jchar*)buffer, length); 
  } 
 } 
 if(buffer) 
 { 
  free(buffer); 
 } 
 return rtn; 


下面這個沒有用過,剛看到,也寫進來,以後如果遇到可以驗證下看。

jstring 轉 char* 或者 const char*
C代碼  [c]
// jstring str 
const char *key = env->GetStringUTFChars(str, 0); 
//jboolean isOffer 
jsClient->modify(key, isOffer); 
env->ReleaseStringUTFChars(str, key); 


JNI 返回 jbyteArray
C代碼  [c]
 JNIEXPORT jbyteArray JNICALL Java_Test_getByteArray(JNIEnv *env, jobject obj) 

    jbyteArray firstMacArray = env->NewByteArray( 6 ); 
    ...... 
    jbyte *bytes = env->GetByteArrayElements( firstMacArray, 0); 
    for ( int i = 0; i < sizeof( pAdapterInfo->Address ); i++ ) 
    { 
       bytes[ i ] = pAdapterInfo->Address[ i ]; 
    } 
 
    env->SetByteArrayRegion(firstMacArray, 0, 6, bytes ); 
    return firstMacArray; 


//jstring to char*
C代碼  [c]
char* jstringTostring(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; 


//char* to jstring
C代碼  [c
jstring stoJstring(JNIEnv* env, const char* pat) 

jclass strClass = env->FindClass("Ljava/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類型轉換成windows類型
C代碼  [c]
char* jstringToWindows( JNIEnv *env, jstring jstr ) 

int length = (env)->GetStringLength(jstr ); 
const jchar* jcstr = (env)->GetStringChars(jstr, 0 ); 
char* rtn = (char*)malloc( length*2+1 ); 
int size = 0; 
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL ); 
if( size <= 0 ) 
return NULL; 
(env)->ReleaseStringChars(jstr, jcstr ); 
rtn[size] = 0; 
return rtn; 

//將windows類型轉換成jstring類型
C代碼  [c] view plaincopy jstring WindowsTojstring( JNIEnv* env, char* str ) 

jstring rtn = 0; 
int slen = strlen(str); 
unsigned short * buffer = 0; 
if( slen == 0 ) 
rtn = (env)->NewStringUTF(str ); 
else 

int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 ); 
buffer = (unsigned short *)malloc( length*2 + 1 ); 
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 ) 
rtn = (env)->NewString( (jchar*)buffer, length ); 

if( buffer ) 
free( buffer ); 
return rtn; 

/*JNIEXPORT jstring JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_strcal
(JNIEnv *env, jclass obj, jstring jstr1, jstring jstr2)
{
jbyteArray bytes = 0;
jthrowable exc;
char *pszResult = NULL;    
char *pszSTR1 = NULL;
char *pszSTR2 = NULL;
 
pszSTR1 = jstringTostring(env, jstr1);
pszSTR2 = jstringTostring(env, jstr2);
 
 
int nlen = sizeof(char)*(strlen(pszSTR1)+strlen(pszSTR2));
pszResult = (char*)malloc(nlen);
strcpy(pszResult, pszSTR1);
strcat(pszResult, pszSTR2);
 
jstring jstrRe = stoJstring(env, pszResult);
free(pszSTR1);
free(pszSTR2);
free(pszResult);
return(jstrRe);
}
*/ 

 

jni object的使用

每一個jni格式的dll中的object對應該java裡面的一個類。
如下例有一個 ObjData類,類中有成員bData ,Len
public class ObjData {
  public byte[]  bData;
  public int Len;       
}
//------------------------jni獲得傳過來的Object類型的變量objDataIn--------

jclass clazz =(env)->FindClass("ObjData");

//從傳進來的對象中取出byte[]
C代碼  [c]
 jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B"); 
jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData); 
jsize theArrayLeng = env->GetArrayLength(pDataIn); 

//byte[]轉為BYTE[]
C代碼  [c]
 jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0);  
BYTE * jDataIn = (BYTE *)arrayBody;  

 

//將BYTE數組轉為jarray
C代碼  [c
 jbyte* byte = (jbyte*)jDataOut;     
jbyteArray jarray = env->NewByteArray(theArrayLeng); 
env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte); 


//給每一個實例的變量付值
C代碼  [c]
(env)->SetObjectField(objDataIn,byteData,jarray); 
(env)->SetIntField(objDataIn,pDataInLen,jDataInLen); 
(env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0);  


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved