字符串的编码和解码

package IO;
//编码:字符串--->字节
//解码:字节--->字符串

import jdk.swing.interop.SwingInterOpUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class ContentEncode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String msg="使命实名十名a";
        //编码:字节数组
        byte[] datas=msg.getBytes();//默认使用工程的字符集 UTF-8中文三个字节 英文一个字节
//        System.out.println(datas.length);
//        //编码:其他字符集
//        datas=msg.getBytes("UTF-16LE");
//        System.out.println(datas.length);//每个都是两个字节
//        datas=msg.getBytes("GBK");
//        System.out.println(datas.length);//中文两个字节英文一个字节
        //解码
        msg=new String(datas,0,datas.length,"utf-8");
        System.out.println(msg);
        //乱码:
        //1)字节数不够
        msg=new String(datas,0,datas.length-2,"utf-8");
        System.out.println(msg);
        msg=new String(datas,0,datas.length-1,"utf-8");
        System.out.println(msg);
        //2)字符集不统一
        msg=new String(datas,0,datas.length-1,"gbk");
        System.out.println(msg);
        //解码时要告诉正确的字节数和正确的字符集
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值