字符编码问题 - Java

58 阅读1分钟


测试用例 - 查看各种编码是否兼容ISO8859-1

public static void main(String[] args) throws IOException {
    String str = new String("fsdfsdfsd");

    // 1. 将str字符以 ISO8859-1 进行编码
    byte[] b_str = str.getBytes("ISO8859-1");

    // 2. 将 ISO8859-1 进行编码的字符以下列各种编码格式进行解码
    String str1 = new String(b_str, "Unicode");
    System.out.println("Unicode解码:" + str1 + "\n");

    String str2 = new String(b_str, "GBK");
    System.out.println("GBK解码:" + str2 + "\n");

    String str3 = new String(b_str, "UTF-8");
    System.out.println("UTF-8解码:" + str3 + "\n");

    String str4 = new String(b_str, "ISO8859-1");
    System.out.println("ISO8859-1解码:" + str4 + "\n");

}


运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-whlI9dCY-1574620723542)(en-resource://database/10323:1)]