编码

编码表

编码表的由来

1.计算机智能识别二进制数据,早期由来是电信号

 2.为了方便应用计算机,让它可以识别各个
    国家的文字。
 3.就将各个国家的文字用数字来表示,并
    一一对应,形成一张表
 4.这就是编码表

常见的编码表

      1.ASCLL:美国标准信息交换码。
                a:用一个字节的7位可以表示。

     2.ISO8859-1:拉丁码表。欧洲码表
                a:用一个字节的8位表示。

     3.GB2321:中国的中文编码表。
    4.GBK:中国的中文编码表升级,融合了更多的
    中文文字符号。

     5.Unicode:国际标准码,融合了多种文字。
                a:所有文字都用两个字节来表示,java语言就是Unicode

     6.UTF-8:最多用三个字节来表示一个字符。

简单编码解码

    public class Io66_1 {
    public static void main(String[] args) throws IOException {
        /*
         * 字符串-->字节数组:编码。
         * 字节数组-->字符串:解码。
         * 
         * 
         * 你好:(GBK码表):      -60 -29 -70  -61
         * 
         * 你好:(utf-8码表):-28  -67  -96  -27  -91  -67  
         * 
         * 
         * 
         * 如果你编错了,解不出来。
         * 如果编对了,解错了,有可能有解。
         * */

        String str="你好";

        //编码:
        byte[] buf=str.getBytes("GBK");
        //getBytes():使用平台的默认字符集将此 String 编码为 byte 序列,
        //并将结果存储到一个新的 byte 数组中


    printBytes(buf);


        //解码
        String s1=new String(buf,"GBK");
         //String(buf,"GBK"):通过使用指定的 charset 解码指定的 byte 数组,
        // 构造一个新的 String。

    //      System.out.println("s1="+s1);



    }

    private static void printBytes(byte[] buf) {
        for(byte b:buf){
            System.out.print(b+"  ");
        }

    }

}

这里写图片描述

编码解码问题_1

    public class Io67_1 {
    public static void main(String[] args) throws IOException {
        /*
         * 如果你编错了,解不出来。
         * 如果编对了,解错了,有可能有解。
         * */
        String str="你好";

        byte[] buf=str.getBytes("gbk");

        String s1=new String(buf,"iso8859-1");

        System.out.println("s1="+s1);

        byte[] buf2=s1.getBytes("iso8859-1");//获取源字节
        String s2=new String(buf2,"GBK");

        System.out.println("s2="+s2);
    }
}

编码解码问题_2

    public class Io68_1 {
    public static void main(String[] args) throws IOException {
        String str="你好";

        byte[] buf=str.getBytes("gbk");

        String s1=new String(buf,"utf-8");

        System.out.println("s1="+s1);

        byte[] buf2=s1.getBytes("utf-8");//获取源字节
        printBytes(buf2);//-17  -65  -67  -17  -65  -67  -17  -65  -67

        String s2=new String(buf2,"GBK");

        System.out.println("s2="+s2);
    }

    private static void printBytes(byte[] buf2) {
        for(byte b:buf2){
            System.out.print(b+"  ");
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值