How to convert jbyteArray to native char* in jni?

后端 未结 1 774
暖寄归人
暖寄归人 2020-12-09 17:40

I am trying to convert a jbyteArray to native c string (char*) in jni? Unfortunately I can\'t find any documentation on how to do that. I\'m invoking a java function with th

相关标签:
1条回答
  • 2020-12-09 18:24

    I believe you would use GetByteArrayElements and ReleaseByteArrayElements. Something like:

    boolean isCopy;
    jbyte* b = GetByteArrayElements(env, arr, &isCopy);
    

    You should be able to cast b to char* at this point in order to access the data in the array. Note that this may create a copy of the data, so you'll want to make sure to release the memory using ReleaseByteArrayElements:

    ReleaseByteArrayElements(env, arr, b, 0);
    

    The last parameter is a mode indicating how changes to b should be handled. 0 indicates that the values are copied back to arr. If you don't want to copy the data back to arr, use JNI_ABORT instead.

    For more details see the JNI Reference.

    0 讨论(0)
提交回复
热议问题