我这里有一个很大的问题。我正在尝试将 byte[] 从 Java 传递到 C++,并且在转换后得到负值。我已经通过在 Java byte[] 中具有唯一字符来确定问题,在转换并执行日志之后,值是 0 或负数。
我尝试使用字符串字符的测试字节 [],它工作正常。
这是我的代码,如果有帮助的话。
爪哇
public static native void SendMessage(byte[] message, int size); //size = message.length
C++
static void SendMessage(JNIEnv *env, jclass cls, jbyteArray array, jint array_length)
{
jbyte* content_array = (env)->GetByteArrayElements(array,NULL);
//*env->GetByteArrayRegion(array,0,array_length,content_array); //tried this as well, same results
LOGD("content:\n");
for (int i=0; i < array_length; i++)
{
LOGD("%d",content_array[i]);
}
//EDIT
SendMessage(client, (uint8_t*)content_array, array_length); //<- could the problem be at the point where I convert it to uint8_t?
(env)->ReleaseByteArrayElements(array,content_array,0);
}
输出
content: 48
content: 23
content: 13
content: 56
content: 0 // <--- the problem starts here
content: -122
content: 0
content: 78
content: 32
content: -28
etc...
..
..
现在,使用一个简单的测试 byte[] Java
String test = "ABC";
byte[] message = test.getBytes();
public static native void SendMessage(byte[] message, int size); //size = message.length
C++
static void SendMessage(JNIEnv *env, jclass cls, jbyteArray array, jint array_length)
{
jbyte* content_array = (env)->GetByteArrayElements(array,NULL);
//*env->GetByteArrayRegion(array,0,array_length,content_array); //tried this as well, same results
LOGD("content:\n");
for (int i=0; i < array_length; i++)
{
LOGD("%d",content_array[i]);
}
(env)->ReleaseByteArrayElements(array,content_array,0);
}
输出
content: 65 //this works perfectly
content: 66
content: 67
谢谢你的帮助。非常感激。