-1

I want to convert unsigned char array to jbytearray and jbytearray to unsigned char array.

Here is what I want to do:

phase 1)

  1. unsigned char data1[] to jbytearray in jni.
  2. jbytearray to byte[] in java
  3. base 64 encoding byte[] to String and save in file.

phase 2)

  1. load String from saved file
  2. base 64 decoding String to byte[]
  3. byte[] to jbtearray
  4. jbytearray to unsinged char data2[] in jni

Here is my question:

unsigned char's range is 0 ~ 256
and byte(in java) -128 ~ 127

So, although there is overflow in convert between unsigned char and byte,

phase1) data1 and phase2) data2 are same correctly without any data loss?

In my opinion, I logged data1 and data2 with

__android_log_print("test", "data1 : %s", data1);

__android_log_print("test", "data2 : %s", data2);

and they looked like same.

but, I can't understand how it looks like same (even if overflow).

And assumed they are same, strangely something is not going well, in other logic.... (Data1 is encrytped with AES and I want to decrypt Data2 with is same as Data1)

ex.

unsigned char data1[32] = { 250, 100, 230, 120, .......};

task phase1) 2, 3 and phase2) 1,2,3

unsigned char data2[32] = { 250, 100, 230, 120, .......}; 

will be same?

if this way is wrong, what can I do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
oldman77
  • 19
  • 4

1 Answers1

0

You should not worry. Essentially, the conversion between the native array and jbyteArray can be considered as reinterpret_cast. As long as yiu do't mix arithmetic operations between Java and native, you are safe.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307