标签:col coding demo imp nbsp 中国 默认 byte gbk
编码和解码
package com.zzp.demo; import java.io.UnsupportedEncodingException; /** * * 字符串到字节 --> 编码 * @author java * */ public class ContentEncode { public static void main(String[] args) throws UnsupportedEncodingException { String msg = "我是中国人"; byte[] datas = msg.getBytes();//默认是工程的字符集 System.out.println(datas.length); //编码成其他的字符集 datas = msg.getBytes("UTF-16LE"); System.out.println(datas.length); datas = msg.getBytes("UTF-8"); System.out.println(datas.length); } }
package com.zzp.demo; import java.io.UnsupportedEncodingException; /** * * 字节数组到字符串 --> 解码 * @author java * */ public class ContentDecode { public static void main(String[] args) throws UnsupportedEncodingException { String msg = "我是中国人"; byte[] datas = msg.getBytes();//默认是工程的字符集 System.out.println(datas.length); //解码:字符串 msg =new String(datas, 0, datas.length, "gbk"); System.out.println(msg); //乱码 //1、字节数不够 msg = new String(datas, 0, datas.length-1, "gbk"); System.out.println(msg); //2、字符集不统一 msg = new String(datas, 0, datas.length, "utf8"); System.out.println(msg); } }
标签:col coding demo imp nbsp 中国 默认 byte gbk
原文地址:https://www.cnblogs.com/zhangzhipeng001/p/9538441.html