String字符编码格式

  1. String.getBytes();//得到一个操作系统默认的编码格式的字节数组.
    String.getBytes(“UTF-8”);//根据指定的decode编码返回某字符串在该编码下的 byte数组表示.
    new String(btye[] b, “UTF-8”);//按照指定的方法编码
    2.编码解码

     String info1="中";
     byte[] info2 = null;
     String info3 = null;
     byte[] b = null; 
     b = info1.getBytes();//得到一个操作系统默认的编码格式的字节数组
     System.out.println("b = " + b);
     try {
     	info2 = info1.getBytes("UTF-8");//方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示
     	info3 = new String(b, "GBK");//按照指定的方法编码
     } catch (UnsupportedEncodingException e1) {
     	// TODO Auto-generated catch block
     	e1.printStackTrace();
     }
     System.out.println("info1 = " + info1);
     System.out.println("info2 = " + info2);
     System.out.println("info3 = " + info3);
     
     byte[] b_gbk = "中".getBytes("GBK");//"中"以GBK编码
     String s_gbk = new String(b_gbk,"UTF-8");//将byte[]解析成UTF-8字符串
     System.out.println(s_gbk);
    

3 ISO8859-1编码

        byte[] b_gbk = "中".getBytes("GBK");
		byte[] b_utf8 = "中".getBytes("UTF-8");
		byte[] b_iso88591 = "中".getBytes("ISO8859-1");
		System.out.println(b_gbk);
		System.out.println(b_utf8);
		System.out.println(b_iso88591);
		String s_gbk = new String(b_gbk, "GBK");
		String s_utf8 = new String(b_utf8, "UTF-8");
		String s_iso88591 = new String(b_iso88591, "ISO8859-1");
		System.out.println(s_gbk);
		System.out.println(s_utf8);
		System.out.println(s_iso88591);

//ISO8859-1编码在组合之后,无法还原"中",因为ISO8859-1编码的编码表中,没有包含汉字字符.
//需要使用ISO8859-1来操作中文,通过中转,可以通过操作别的编码格式转换后的字节数组,

		byte[] b_utf8 = "中".getBytes("UTF-8");
		String s_iso88591 = new String(b_utf8, "ISO8859-1");
		System.out.println(s_iso88591);
		byte[] bytes = s_iso88591.getBytes("ISO8859-1");
		s_iso88591 = new String(bytes,"UTF-8");
		System.out.println(s_iso88591);
		
		String b_iso = new String("中".getBytes("UTF-8"), "ISO8859-1");
		System.out.println(b_iso);
		String s_iso = new String(b_iso.getBytes("ISO8859-1"), "UTF-8");
		System.out.println(s_iso);

你可能感兴趣的:(String字符编码格式)