jstring和char*之间的转换方法
//jstring to char *
char * jstring tostring(JNIEnv * env,jstring jstr)
{
char * RTN = NULL;
jclass cls String = env-> find class(" Java/lang/String ");
jstring stren code = env-> newstring utf(" utf-8 ");
jmethodID mid = env-> get method id(cls String," getBytes " ,"(Ljava/lang/String;)【B】;
jbyteArray Barr =(jbyteArray)env-> CallObjectMethod(jstr,mid,stren code);
jsize alen = env-> GetArrayLength(Barr);
jbyte * ba = env-> GetByteArrayElements(巴尔,JNI _ FALSE);
if(alen > 0)
{
RTN =(char *)malloc(alen+1);
memcpy(rtn,ba,alen);
RTN[alen]= 0;
}
env-> ReleaseByteArrayElements(Barr,ba,0);
返回rtn
}
0条评论