java怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串

Python016

java怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串,第1张

通过JDK1.6知道String类中getBytes(”编码“)方法可以讲一个数用指定的编码转成一个字节数组,String中通过指定的 charset解码指定的 byte 数组,构造一个新的 String。代码如下:

try{

String s = "java学习"

System.out.println(s)

String result = new String(s.getBytes("GB2312"),"iso-8859-1")

System.out.println(s)

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

扩展资料:

getBytes() 方法有两种形式:

1、getBytes(String charsetName): 使用指定的字符集字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

2、getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

参考资料:

百度百科-getBytes()

public static void main(String[] args) {\x0d\x0a // TODO Auto-generated method stub\x0d\x0a String s = "123abc中国 "\x0d\x0a try {\x0d\x0a byte[] b = s.getBytes()\x0d\x0a String str = " "\x0d\x0a for (int i = 0i 2)\x0d\x0a strTmp = strTmp.substring(strTmp.length() - 2)\x0d\x0astr = str + strTmp\x0d\x0a }\x0d\x0a System.out.println(str.toUpperCase())\x0d\x0a } catch (Exception e) {\x0d\x0a e.printStackTrace()\x0d\x0a }\x0d\x0a }