2012-12-09 39 views
0

可能重複:
Why does byteArray have a length of 22 instead of 20?的Java:字符串和字節之間的轉換[]

我只是不能算出這個爲什麼會出現轉換這樣

String mystring = "This is a String"; 
Log.v("string:", mystring); 
Log.v("byte then string:", mystring.getBytes().toString()); 
時出錯

輸出如下

String: This is a string 
byte then string: [[email protected] 

任何人都可以幫忙嗎?我其實只是想要一個字符串轉換爲字節,然後再轉換爲字符串。

回答

0

你應該使用

Log.v("byte then string:", new String(mystring.getBytes()));

+0

不,你不應該 - 將使用平臺默認的編碼,這幾乎是不合適的。 –

+1

假設使用的方法是byte [] bytes = String.getBytes()',否則應該使用'new String(bytes)',否則應該在第一個字符串中使用String.getBytes(「UTF-8」)地點。 – aymeric

+0

@JonSkeet是的,這是真的,我舉了一個例子。將字節數組轉換爲字符串時,應始終指定編碼。新的字符串(byteArray,「UTF-8」)。 – Subin

相關問題