各种编码转化中文乱码
0
今天无聊搞了一下中文不同编码间相互转化时,出现的乱码的样子,主要用了GBK
/UTF-8
/GB2312
和ISO-8859-1
,结果如下:
编码: GBK ==> GBK = 对啊这就是一个测试啊逗比
编码: GBK ==> UTF-8 = �������һ����������
编码: GBK ==> GB2312 = 对啊这就是一个测试啊逗比
编码: GBK ==> ISO-8859-1 = ¶Ô°¡Õâ¾ÍÊÇÒ»¸ö²âÊÔ°¡¶º±È
编码: UTF-8 ==> GBK = 瀵瑰晩杩欏氨鏄竴涓祴璇曞晩閫楁瘮
编码: UTF-8 ==> UTF-8 = 对啊这就是一个测试啊逗比
编码: UTF-8 ==> GB2312 = 瀵瑰��杩�灏辨��涓�涓�娴�璇�����姣�
编码: UTF-8 ==> ISO-8859-1 = 对åè¿å°±æ¯ä¸ä¸ªæµè¯åéæ¯
编码: GB2312 ==> GBK = 对啊这就是一个测试啊逗比
编码: GB2312 ==> UTF-8 = �������һ����������
编码: GB2312 ==> GB2312 = 对啊这就是一个测试啊逗比
编码: GB2312 ==> ISO-8859-1 = ¶Ô°¡Õâ¾ÍÊÇÒ»¸ö²âÊÔ°¡¶º±È
编码: ISO-8859-1 ==> GBK = ????????????
编码: ISO-8859-1 ==> UTF-8 = ????????????
编码: ISO-8859-1 ==> GB2312 = ????????????
编码: ISO-8859-1 ==> ISO-8859-1 = ????????????
代码很简单:
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class T {
class Encoding {
public String source;
public String target;
public Encoding(String source, String target) {
this.source = source;
this.target = target;
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
T t = new T();
List<Encoding> encodings = new ArrayList<T.Encoding>();
encodings.add(t.new Encoding("GBK", "GBK"));
encodings.add(t.new Encoding("GBK", "UTF-8"));
encodings.add(t.new Encoding("GBK", "GB2312"));
encodings.add(t.new Encoding("GBK", "ISO-8859-1"));
encodings.add(t.new Encoding("UTF-8", "GBK"));
encodings.add(t.new Encoding("UTF-8", "UTF-8"));
encodings.add(t.new Encoding("UTF-8", "GB2312"));
encodings.add(t.new Encoding("UTF-8", "ISO-8859-1"));
encodings.add(t.new Encoding("GB2312", "GBK"));
encodings.add(t.new Encoding("GB2312", "UTF-8"));
encodings.add(t.new Encoding("GB2312", "GB2312"));
encodings.add(t.new Encoding("GB2312", "ISO-8859-1"));
encodings.add(t.new Encoding("ISO-8859-1", "GBK"));
encodings.add(t.new Encoding("ISO-8859-1", "UTF-8"));
encodings.add(t.new Encoding("ISO-8859-1", "GB2312"));
encodings.add(t.new Encoding("ISO-8859-1", "ISO-8859-1"));
String textContent = "对啊这就是一个测试啊逗比";
for (Encoding encoding : encodings) {
System.out.print("编码: " + encoding.source + " ==> " + encoding.target + " = ");
System.out.println(new String(textContent.getBytes(encoding.source), encoding.target));
}
}
}