我使用字符串進行字節數組傳輸,但發現有些奇怪的東西。有人可以解釋,爲什麼會發生這種情況?在字符串換行後字節不相等
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
- 這是triple des secret key bytes數組。每次它不同,但只有在案例中,如果bytes1
包含-104
值,它纔會失敗。非常感謝。
請說出你希望這段代碼輸出什麼,以及它輸出什麼。 「奇怪的東西」不是一個特定的錯誤。 –
我在'System.out.println'行添加註釋 – degr
我得到虛假的錯誤檢查它在這裏https://ideone.com/zpPHNg – SpringLearner