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));
}
}
本文探讨了编码和解码的基本过程,从字符到编码字符集再到二进制的转换,以及从二进制通过解码字符集还原为字符的逆过程。同时,重点解析了乱码产生的两个主要原因:编码与解码时使用的字符集不一致,以及字节丢失导致的长度问题。
10万+

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



