I'm trying to convert byte[] to String and than String to byte[]. I retrive not the same byte[] array.
byte[] bArray1 = myFunction();
System.out.println("array1 = " + bArray1.toString());
String str = new String(bArray1);
byte[] bArray2 = str.getBytes();
System.out.println("array2 = " + bArray2.toString());
After executing i get:
array1 = [-15, -87, -44, 61, -115, 23, -3, 75, 99, 36, -49, 21, -41, -63, 100, -49]
array2 = [-17, -65, -67, -17, -65, -67, 61, -17, -65, -67, 23, -17, -65, -67, 75, 99, 36, -17, -65, -67, 21, -17, -65, -67, -17, -65, -67, 100, -17, -65, -67, -17, -65, -67]
Why does it happen and how can i get the same array?
This work on my computer, but not on my android:
byte[] bArray1 = myFunction();
String str = Base64.encodeToString(bArray1, Base64.DEFAULT);
byte[] bArray2 = Base64.decode(str, Base64.DEFAULT);
I have seen the article Hex-encoded String to Byte Array. But android have not class Hex.
Edited
I'm sorry, i was wrong that Base64 is not working.
This was tested at android 2.3.3, 2.3.4, 4.2, 4.3 and it works:
byte[] bArray1 = myFunction();
String str = Base64.encodeToString(bArray1, Base64.DEFAULT);
byte[] bArray2 = Base64.decode(str, Base64.DEFAULT);