JNI converting native unsigned char arry to jbyte array

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

The C struct is defined as followed:

// 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:

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

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!

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&)

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!


env->SetByteArrayRegion(Array, 0, 28, (jbyte*)SendData.bytes)
链接地址: http://www.djcxy.com/p/54082.html

上一篇: C ++读取文件末尾的剩余数据

下一篇: JNI将原生的无符号字符转换为jbyte数组