The Conversion Of JNI

1.j***Array<->***[] (c++,not C)

you can use

jsize theArrayLength = env->GetArrayLength(arraySrc);

to get the length of the array;

jbyteArray->BYTE[]

#define BYTE unsigned char
jbyte * arrayTemp=env->GetByteArrayElements(arraySrc,0);//arraySrc is jbyteArray 
BYTE *arrayRet= (BYTE *)arrayTemp;

or

#define BYTE unsigned char
BYTE *arrayRet= (BYTE *)env->GetByteArrayElements(arraySrc,0);//arraySrc is jbyteArray

BYTE[]>jbyteArray

jbyte *arrayTemp = (jbyte*)arraySrc;  //arraySrc is BYTE[] 
jbyteArray arrayRet = env->NewByteArray(arraySize);  
env->SetByteArrayRegin(jarray, 0, nOutSize, arrayTemp );

or

jbyteArray arrayRet = env->NewByteArray(arraySize);  
env->SetByteArrayRegin(jarray, 0, nOutSize,  (jbyte*)arraySrc );

jbyteArray–>char *

char* arrayRet= (char*)env->GetByteArrayElements(arraySrc, 0);

jintArray->int[]

jint * arrayTemp= env->GetIntArrayElements(arraySrc,0);//arraySrc is jintArray
int * arrayRet= (int *)arrayTemp;

2.String

char*->jstring

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*

1.
const char *key = env->GetStringUTFChars(str, 0);  
env->ReleaseStringUTFChars(str, key);

2.
jint
Java_com_wzh_test_AndriodJNISampleActivity_add( JNIEnv* env,jobject this,jstring path)
{

  const char *folder = (*env)->GetStringUTFChars(env, path, 0);

  (*env)->ReleaseStringUTFChars(env, path, folder);

  ......

}

 

jstring to char*  (not mine)

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(not mine)

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);  
}

3.

JNI return jbyteArray

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;  
}

or you can create a byte[] first ,fill it ,and assign it to firstMacArray by SetByteArrayRegion

BYTE *temp=new BYTE[10];
.......
env->SetIntArrayRegion(firstMacArray, 0,arraySize, (jbyte *)temp);

get byte[] from the coming object

jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B");  
jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData);  
jsize theArrayLeng = env->GetArrayLength(pDataIn);

assign values to a variables of a object

(env)->SetObjectField(objDataIn,byteData,jarray);  
(env)->SetIntField(objDataIn,pDataInLen,jDataInLen);  
(env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0);
posted @ 2012-07-27 17:57  Qiengo  阅读(1269)  评论(0编辑  收藏  举报