I use string for byte array transferring, but found something strange with it. Can somebody explain, why that's happen?
byte[] bytes1 = new byte[]{-104, 73, 61, -15, -92, 109, 62, -99, 50, 82, 26, 87, 38, 110, -12, 49, -104, 73, 61, -15, -92, 109, 62, -99};
byte[] bytes2 = new String(bytes1).getBytes();
//for now bytes2 is equal to: {63, 73, 61, -15, -92, 109, 62, -99, 50, 82, 26, 87, 38, 110, -12, 49, 63, 73, 61, -15, -92, 109, 62, -99}
System.out.println(Arrays.equals(bytes1, bytes2));//false
for(int i = 0; i < bytes2.length; i++){
if(bytes2[i] == 63) {
bytes2[i] = -104;
}
}
System.out.println(Arrays.equals(bytes1, bytes2));//true
ps bytes1
- this is triple des secret key bytes array. Each time it different, but it fail only in case, if bytes1
contain -104
values. Many thanks.