1.编码与解码概念 编码:字符–>编码字符集–>二进制 解码:二进制–>解码字符集–>字符
2.乱码 1)编码与解码字符集不统一 2)字节缺少,长度丢失
package com.et.io.convert; import java.io.UnsupportedEncodingException; public class ConvertDemo01 { public static void main(String[] args) throws UnsupportedEncodingException { String str ="中国"; byte[] data =str.getBytes(); //字节数不完整 System.out.println(new String(data,0,3)); } public static void test1() throws UnsupportedEncodingException { //解码byte-->char String str="中国"; //gbk //编码char --> byte byte[] data =str.getBytes(); //编码与解码字符集统一 System.out.println(new String(data)); //不统一字符集出现乱码 data=str.getBytes("UTF-8");//设定编码字符集 System.out.println(new String(data)); //编码 byte[] data2 = "中国".getBytes("UTF-8"); //解码 str=new String(data2,"UTF-8"); System.out.println(new String(str)); } }