JAVA/JNI的jstring转换为char*

具体代码:

#define CHARSET_UTF8 "utf8"
 
static jclass jClassString = NULL;
 
void jstringToBuffer(JNIEnv* pEnv, jstring jresult, char** ppBuffer)
 
{
    if (jClassString == NULL)
    {
        jClassString = pEnv->FindClass("java/lang/String");
    }
 
    jmethodID  jmethod = pEnv->GetMethodID(jClassString, "getBytes", "(Ljava/lang/String;)[B");
    jbyteArray jarr    = (jbyteArray)pEnv->CallObjectMethod(jresult, jmethod, pEnv->NewStringUTF(CHARSET_UTF8));
    jsize      jlen    = pEnv->GetArrayLength(jarr);
    jbyte*     jdata   = pEnv->GetByteArrayElements(jarr, JNI_FALSE);
 
    if (*ppBuffer == NULL)
    {
        *ppBuffer = (char*)malloc((int)jlen+1);
        memset(*ppBuffer, 0, (int)jlen +1);
    }
    strncpy(*ppBuffer, (char*)jdata, (int)jlen);
 
    pEnv->ReleaseByteArrayElements(jarr, jdata, 0);
 
}
上一篇:惠普商业智能现代化服务加速企业运用大数据与分析实现价值


下一篇:独家解读 etcd 3.4版本 |云原生生态周报 Vol. 18