简体   繁体   English

将JNI Java byte []转换为C ++ bytearray,返回0

[英]Converting JNI Java byte[] to C++ bytearray, returning 0

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. 我试图将一个byte []从Java传递给C ++,转换后我得到负值。 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. 我已经确定了Java字节[]中包含唯一字符的问题,在转换和执行日志之后,值为0或负数。

I have tried using a test byte[] of String characters and it works fine. 我已经尝试使用字符串字符的测试字节[],它工作正常。

Here is my code, if it helps. 这是我的代码,如果它有帮助。

Java Java的

public static native void SendMessage(byte[] message, int size); //size = message.length

C++ 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 现在,使用一个简单的测试byte [] Java

String test = "ABC";
byte[] message = test.getBytes();
public static native void SendMessage(byte[] message, int size); //size = message.length 

C++ 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? 如何在问题情况下获得byte[]数组? Is that also a conversion from a String ? 这也是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. 如果您使用String.getBytes()与简单文本一样,则将使用平台默认编码。 Your simple case shows that the default encoding is something ASCII-compatible. 您的简单案例表明默认编码与ASCII兼容。

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. 在Java中, byte是有符号类型,因此负值不是意外的。 Jbyte is presumably an 8-bit signed C++ type to match. Jbyte可能是一个匹配的8位带符号C ++类型。

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 ...) 例如,你已经用UTF-8编码(尽管零会指示其他情况......)

  • You've gotten the value of the size parameter incorrect: 你得到size参数的值不正确:

    • 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. 将内容写入字节数组的过程没有将size字节放入其中。


It is worth noting that your JNI code doesn't check that 0 <= size < message.length . 值得注意的是,您的JNI代码不会检查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. 如果使用超出范围的size参数调用此方法,则可能发生错误的事情......包括分段错误,这将导致硬JVM崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM