我試圖將字節[]轉換爲字符串,並將字符串轉換爲字節[]。我回顧不是相同的byte []數組。字節數組 - >字符串 - >字節數組
byte[] bArray1 = myFunction();
System.out.println("array1 = " + bArray1.toString());
String str = new String(bArray1);
byte[] bArray2 = str.getBytes();
System.out.println("array2 = " + bArray2.toString());
執行後我得到:
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]
它爲什麼會發生,我怎麼能得到相同的陣列?
我的電腦上這項工作,但不能在我的Android:
byte[] bArray1 = myFunction();
String str = Base64.encodeToString(bArray1, Base64.DEFAULT);
byte[] bArray2 = Base64.decode(str, Base64.DEFAULT);
我所看到的文章Hex-encoded String to Byte Array。 但android沒有class Hex。
編輯
對不起,我錯了是Base64是行不通的。
這在安卓2.3.3,2.3.4,4.2,4.3進行了測試,它的工作原理:
byte[] bArray1 = myFunction();
String str = Base64.encodeToString(bArray1, Base64.DEFAULT);
byte[] bArray2 = Base64.decode(str, Base64.DEFAULT);
它們是相同的不同的字符編碼字符串 – tom
建立在湯姆的評論,你在處理從myFunction()返回的byte []中的實際ASCII /可打印數據?你想通過轉換來做什麼? –
[String to Byte Array]的可能重複(http://stackoverflow.com/questions/6650650/string-to-byte-array) – alfasin