I've a really big problem here. I am trying to pass a byte[] from Java to C++ and I am getting negative values after the conversion. I have determined the problem from having unique characters in the Java byte[] which after converting and doing a log, the values are either 0 or negative.
I have tried using a test byte[] of String characters and it works fine.
Here is my code, if it helps.
Java
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);
}
Output
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...
..
..
Now, using a simple test 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);
}
Output
content: 65 //this works perfectly
content: 66
content: 67
Thanks for your help. Much appreciated.
How are you obtaining the byte[]
array in the problem case? Is that also a conversion from a String
? If so, getting zeros and negative values in your log output may be perfectly valid. It depends on the input characters and the encoding you are using to convert to a byte array. If you are using String.getBytes()
as with your simple text, you will be using the platform default encoding. Your simple case shows that the default encoding is something ASCII-compatible.
I'm not sure what you think the problem is here. In Java, byte
is a signed type, so a negative value is not unexpected. Jbyte is presumably an 8-bit signed C++ type to match.
The most likely explanations are:
This is some artefact of the way that you are creating the byte array; eg you have encoded in UTF-8 (though the zero would tend to indicate otherwise ...)
You've gotten the value of the size
parameter incorrect:
For some reason it is larger than the byte array size.
The process that wrote stuff into the byte array didn't put size
bytes into it.
It is worth noting that your JNI code doesn't check that 0 <= size < message.length
. If this method is called with a size
argument that is out of range, bad things could happen ... including segmentation faults, that would lead to a hard JVM crash.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.