java 编码

String ISO = "ISO-8859-1";
String UTF = "UTF-8";
String GBK = "GBK";
String string = "很开心分享经验";
/*ISO*/
byte[] bytes = string.getBytes(ISO);
System.out.println("结果:"+new String(bytes,ISO));
//結果为7个"?"
for(byte b:bytes)
System.out.print(b+" ");
//结果为7个"63"
/*解释:由于iso8859-1不认识中文字符,统统用63表示(该编码将"?"编码为"63"
* 而英文不涉及编码(英文用ASCII,而iso8859-1,utf8,GBK几种编码兼容ASCII)
* */
/*UTF*/
bytes = string.getBytes(UTF);
System.out.println("结果:"+new String(bytes,UTF));
//结果:很开心分享经验
for(byte b:bytes)
System.out.print(b+" ");
//结果为:-27 -66 -120 -27 -68 -128 -27 -65 -125 -27 -120 -122 -28 -70 -85 -25 -69 -113 -23 -86 -116
/*GBK(也正常)*/
bytes = string.getBytes(GBK);
System.out.println("结果:"+new String(bytes,GBK));
//遇到一大堆"?",一般可以确定用了ISO

/*其他类型编码:
1. 寰堝紑蹇冨垎浜粡楠� UTF-GBK
2. �ܿ��ķ��?�� GBK-UTF
3. ºÜ¿ªÐÄ·ÖÏí¾Ñé GBK-ISO
4. å¾ˆå¼€å¿ƒåˆ†äº«ç»éª UTF-ISO
5 裥벀菥袆ꯧ뮏� utf-8--utf-16
* */

posted @ 2017-12-29 12:37  wonkju  阅读(218)  评论(0编辑  收藏  举报