Java String byte數組 字符集轉換

轉自:http://blog.csdn.net/aaronuu/article/details/7007386

1,byte[] str.getBytes(String charsetName) 


將str按指定的字符集charsetName解碼爲字符數組.

返回用指定的字符集charsetName解碼後的字符數組。


2,String(byte[] bytes, String charsetName) 


構造一個新的String.將字符數組bytes按着指定的字符集charsetName指定的字符集進行編碼。


3, String str = "您好";
String newStr = new String(str.getBytes("GB2312"),"ISO-8859-1");
這句話的意思是把str用GB2312編碼方式取出,將取出的字符數組用ISO-8859-1再進行編碼,來構造String類型對象newStr
相當於:
String str = "您好";
byte[] tbyte = str.getBytes("GB2312");//str用GB2312編碼方式取出
String newStr = new String(tbyte,"ISO-8859-1");//將tbyte轉換爲ISO-8859-1編碼形式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章