您的位置:首页 > 其它

字符编码问题

2016-03-04 15:59 375 查看
ASCII  美国标准编码表

ISO8859-1 :拉丁编码表

GB2312 :中文编码

GBK和GB18030:兼容GB2312

Unicode:国际标准编码表

UFT-8:支持中文编码





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);
}

}


运行结果:





 

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);                   // 打印出正确的结果
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: