[IO]——编码和解码

来源:互联网 发布:淘宝 新店扶持 编辑:程序博客网 时间:2024/06/06 01:05
public class Conver {public static void main(String[] args) {String str="中国";byte[] date=str.getBytes();//字节数不完整System.out.println(new String(date,0,3));}/** * 编码和解码字符集必须相同,否则乱码 * @throws UnsupportedEncodingException */public static void test() throws UnsupportedEncodingException {//解码byte-->charString str="中国";//gbk//编码char-->bytebyte[] date=str.getBytes();//编码和解码字符集统一System.out.println(new String(date));date=str.getBytes("utf-8");//设定编码字符集//不同一出现乱码System.out.println(new String(date));/////////////////编码byte[] date2="中国".getBytes("utf-8");//解码str=new String(date2, "utf-8");System.out.println(str);}}

0 0
原创粉丝点击