1,byte[] str.getBytes(String charsetName)
返回用指定的字符集charsetName解码后的字符数组。
2,String(byte[] bytes, String charsetName)
构造一个新的String.将字符数组bytes按着指定的字符集charsetName指定的字符集进行编码。
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编码形式
本文介绍了如何使用Java中的String类和getBytes方法实现不同字符集间的编码转换,包括从特定字符集编码到字节数组,再到另一种字符集的解码过程。
1417

被折叠的 条评论
为什么被折叠?



