Java JNI 将本机无符号字符数组转换为 jbyte 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19705541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:33:30  来源:igfitidea点击:

JNI converting native unsigned char arry to jbyte array

javaandroidc++arraysjava-native-interface

提问by Roy08

í have JNI function that needs to return a jbyte array. The array contains byte data of a c struct with data. But i get an error when i pass the data form the unsinged char array to the jbyte array.

í 有需要返回 jbyte 数组的 JNI 函数。该数组包含带有数据的 ac 结构的字节数据。但是当我将数据从 unsinged char 数组传递到 jbyte 数组时出现错误。

The C struct is defined as followed:

C 结构体定义如下:

// ObjectInfo struct definition
 struct ObjectInfo {
    int ObjectXCor;
    int ObjectYCor;
    int ObjectMass;
 };

 // ObjectInfo struct definition
 struct SensorDataStruct{
    int PingData;
    int IRData;
    int ForceData;
    int CompassData;
 };

 // ObjectInfo struct definition
 union PackedSend{
    struct CommStruct{
        ObjectInfo VisionData;
        SensorDataStruct SensorData;
    } CommData;
    unsigned char bytes[28];
 }SendData;

The JNI method is defined as followed:

JNI方法定义如下:

JNIEXPORT jbyteArray JNICALL Java_com_example_communicationmodule_MainActivity_Convert(
    JNIEnv *env, jobject,
    jint var1,
    jint var2,
    jint var3,
    jint var4,
    jint var5,
    jint var6,
    jint var7)
{
// Array to fill with data
jbyteArray Array;

// Init  java byte array
Array = env->NewByteArray(28);

SendData.CommData.SensorData.PingData = var1;
SendData.CommData.SensorData.IRData = var2;
SendData.CommData.SensorData.ForceData = var3;
SendData.CommData.SensorData.CompassData = var4;
SendData.CommData.VisionData.ObjectXCor = var5;
SendData.CommData.VisionData.ObjectYCor = var6;
SendData.CommData.VisionData.ObjectMass = var7;

//Put the native unsigned chars in the java byte array
for(int Index=0; Index < 28; Index++){
    Array[Index] = SendData.bytes[Index];

}


// Return java array
return Array;
}

The error that i get is: jni/HelperFunctions.cpp:44:38: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = SendData.PackedSend::bytes[Index]'

我得到的错误是:jni/HelperFunctions.cpp:44:38: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = SendData.PackedSend::bytes[Index] '

My question is wat is the proper way to convert the native unsigned char array to the a jbyte array? Doe anyboy have a suggestion? All tips are welcome!

我的问题是 wat 是将本机无符号字符数组转换为 jbyte 数组的正确方法吗?有大佬给点建议吗?欢迎所有提示!

Update

更新

After casting to (jbyte) i get the the following errors: jni/HelperFunctions.cpp:54:46: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = (jbyte)SendData.PackedSend::bytes[Index]' jni/HelperFunctions.cpp:54:46: note: candidate is: C:/android-ndk-r9/platforms/android-8/arch-arm/usr/include/jni.h:66:7: note: _jbyteArray& _jbyteArray::operator=(const _jbyteArray&)

转换为 (jbyte) 后,我收到以下错误:jni/HelperFunctions.cpp:54:46: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = (jbyte) SendData.PackedSend::bytes[Index]' jni/HelperFunctions.cpp:54:46:注意:候选是:C:/android-ndk-r9/platforms/android-8/arch-arm/usr/include/jni。 h:66:7: 注意:_jbyteArray& _jbyteArray::operator=(const _jbyteArray&)

The casting code is as followed:

铸造代码如下:

//Put the native unsigned chars in the java byte array
for(int Index=0; Index < 28; Index++){
    Array[Index] = (jbyte) SendData.bytes[Index];
}

Does anybody have a idea or suggestion? Everthing is welcome!

有人有想法或建议吗?什么都欢迎!

采纳答案by afpro

env->SetByteArrayRegion(Array, 0, 28, (jbyte*)SendData.bytes)