jni和java之间字符串的转换

举报
风吹稻花香 发表于 2021/06/04 23:18:14 2021/06/04
【摘要】  jni和java之间字符串的转换方法。   C的实现:   JNIEXPORT jstring JNICALL Java_Android123_CwjC (JNIEnv *env, jobject obj, jstring string)   {    char szBuffer[255];    return (*env)->NewS...

 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[]


//jbytearray array



jbyte * olddata = (jbyte*)env->GetByteArrayElements(array, 0);




jsize  oldsize = env->GetArrayLength(array);



BYTE* bytearr = (BYTE*)olddata;




int len = (int)oldsize;



//C++中的BYTE[]转jbyteArray




//nOutSize是BYTE数组的长度 BYTE pData[]




jbyte *by = (jbyte*)pData;



jbyteArray jarray = env->NewByteArray(nOutSize);


env->SetByteArrayRegin(jarray, 0, nOutSize, by);


//jbyteArray 转 char *


char* data = (char*)env->GetByteArrayElements(strIn, 0);


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


    }


11


    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;




}


 


1


//jstring 转 char* 或者 const char*


// jstring str

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

//jboolean isOffer


jsClient->modify(key, isOffer);


//需要释放,否则容易内存泄露


env->ReleaseStringUTFChars(str, key);


//JNI 返回 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;

}


//jstring to char*



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


jstring stoJstring(JNIEnv* env, const char* pat)



{


    jclass strClass = env->FindClass("Ljava/lang/String;");


    jmethodID ctorID = env->GetMethodID(strClass, "", "([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类型


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类型



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;


}


//从传进来的对象中取出byte[]


jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B");


jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData);


jsize theArrayLeng = env->GetArrayLength(pDataIn);


//byte[]转为BYTE[]


jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0);


BYTE * jDataIn = (BYTE *)arrayBody;


 JNI开发交流 C/C++,JAVA,android,linux/unix  QQ群:25093009


//将BYTE数组转为jarray


jbyte* byte = (jbyte*)jDataOut;   



jbyteArray jarray = env->NewByteArray(theArrayLeng);

env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte);


//给每一个实例的变量付值


(env)->SetObjectField(objDataIn,byteData,jarray);


(env)->SetIntField(objDataIn,pDataInLen,jDataInLen);


(env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0);

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/61419570

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。