为了账号安全,请及时绑定邮箱和手机立即绑定

将 const unsigned char* 转换为 jbyteArray,反之亦然

将 const unsigned char* 转换为 jbyteArray,反之亦然

阿晨1998 2023-11-01 21:02:15
我有两个问题:如何将 const unsigned char* 转换为 jbyteArray?JNIEXPORT jbyteArray JNICALL Java_MyJavaApplication_readValue(JNIEnv* env, jobject obj){    const unsigned char* someConstUnsignedChar = (gcnew MyCSharpLibrary())->readValue();    // What should be here?    return someJbyteArray;}如何将 jbyteArray 转换为 const unsigned char*?JNIEXPORT jboolean JNICALL Java_MyJavaApplication_writeValue(JNIEnv* env, jobject obj, jbyteArray someJbyteArray){    // What should be here?    return (gcnew MyCSharpLibrary())->writeValue(someConstUnsignedChar);}
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

将 const unsigned char* 转换为 jbyteArray:


#include <tuple>


JNIEXPORT jbyteArray JNICALL Java_MyJavaApplication_readValue(JNIEnv* env, jobject obj)

{

    const unsigned char* someConstUnsignedChar;

    int len;

    std::tie(someConstUnsignedChar, len) = (gcnew MyCSharpLibrary())->readValue();


    jbyteArray someJbyteArray = env->NewByteArray((jsize)len);

    env->SetByteArrayRegion(someJbyteArray, 0, len, reinterpret_cast<const jbyte*>(someConstUnsignedChar));


    return someJbyteArray;

}

将 jbyteArray 转换为 const unsigned char*:


JNIEXPORT jboolean JNICALL Java_MyJavaApplication_writeValue(JNIEnv* env, jobject obj, jbyteArray someJbyteArray)

{

    jsize len = env->GetArrayLength(someJbyteArray);

    jbyte* body = env->GetByteArrayElements(someJbyteArray, 0);

    unsigned char* someUnsignedChar = new unsigned char[len];

    for (jint i = 0; i < len; i++)

    {

        someUnsignedChar[i] = (unsigned char)body[i];

    }


    bool result = (gcnew MyCSharpLibrary())->writeValue((const unsigned char*)someUnsignedChar);


    env->ReleaseByteArrayElements(someJbyteArray, body, 0);

    return (jboolean)result;

}

这个对我有用。


查看完整回答
反对 回复 2023-11-01
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信