例:
package com.study;
import java.io.UnsupportedEncodingException;
/**
* 测试字符集
* @author Administrator
*
*/
public class ContenDecode {
public static void main(String[] args) throws UnsupportedEncodingException {
String msg = "不i测验完毕";
//编码字节数组
byte[] datas = msg.getBytes();
System.out.println(datas.length);
//编码其他字符,可以随意指定字符集
datas = msg.getBytes("UTF-8");
System.out.println(datas.length);
//解码:字符串
String mgs = new String(datas,0,datas.length,"UTF-8");
System.out.println(mgs);
//乱码原因:
//1.字节数不够:
String mmm = new String(datas,0,datas.length-2,"UTF-8");
System.out.println(mmm);
//2.字符集不统一
mgs = new String(datas,0,datas.length,"GBK");
System.out.println(mgs);
}
}
原文:https://www.cnblogs.com/LuJunlong/p/11674089.html