字符编码问题

ASCII  美国标准编码表

ISO8859-1 :拉丁编码表

GB2312 :中文编码

GBK和GB18030:兼容GB2312

Unicode:国际标准编码表

UFT-8:支持中文编码

image

package com.example.io;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Example32 {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String str="北京";
        byte[] b1=str.getBytes();                //使用默认的编码表
        byte[] b2=str.getBytes("GBK");            //使用GBK编码表
        System.out.println(Arrays.toString(b1));  // 打印出字节数组的字符串形式
        System.out.println(Arrays.toString(b2));  //系统默认就是GBK
        byte[] b3 = str.getBytes("UTF-8");             // 使用UTF-8编码
        //gbk的编码gbk编码输出(系统默认)
        String result1 = new String(b1, "GBK"); 
        System.out.println(result1);
        //gbk的编码gbk编码输出
        String result2 = new String(b2, "GBK");
        System.out.println(result2);
        //utf-8编码UFT-8输出
        String result3 = new String(b3, "UTF-8");
        System.out.println(result3);
        //gbk的编码ISO8859-1编码输出
        String result4 = new String(b2, "ISO8859-1");
        System.out.println(result4);
    }

}

运行结果:

image

import java.util.*;
public class Example33 {
    public static void main(String[] args) throws Exception {
        String str = "北京";
        byte[] b = str.getBytes("GBK");
        String temp = new String(b, "ISO8859-1");
        System.out.println(temp);                      // 用错误的码表解码,打印出了乱码
        byte[] b1 = temp.getBytes("ISO8859-1");     // 再使用错误的码表编码
        String result = new String(b1, "GBK");      // 用正确的码表解码
        System.out.println(result);                   // 打印出正确的结果
    }
}
加油啦!加油鸭,冲鸭!!!
原文地址:https://www.cnblogs.com/clarencezzh/p/5242421.html