首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

还是编码有关问题

2012-02-19 
还是编码问题publicstaticvoidmain(String[]args){Stringstr英 byte[]utfstr.getBytes( UTF-8String

还是编码问题
public   static   void   main(String[]   args)   {
String   str   =   "英 ";
byte[]   utf   =   str.getBytes( "UTF-8
String   strUTF   =   new   String(utf);
byte[]   u   =   strUTF.getBytes( "GBK ");
String   stru   =   new   String(u, "UTF-8 ");
System.out.println(str);
System.out.println(strUTF);
System.out.println(stru);
}

我是中文系统,结果是

鑻?
??

而我的理解是,一开始把“英”按utf-8编码存入字节数组utf,然后再按平台默认的GBK转成串strUTF,那么strUTF自然就是乱码了,此时,又按GBK把strUTF反编存入字节数组u,然后再按utf-8转换成stru,那么结果应该是

鑻?


但为什么实际上运行时是

鑻?
??

?????????

[解决办法]
public static void main(String[] args) {
String str = "英 ";
byte[] utf = str.getBytes( "UTF-8
String strUTF = new String(utf);

<== 将utf字节转换为GB编码的字符串的时候,由于存在字符无法映射,默认去字节码为63的‘?’,因此存在信息丢失!


byte[] u = strUTF.getBytes( "GBK ");
String stru = new String(u, "UTF-8 ");
System.out.println(str);
System.out.println(strUTF);
System.out.println(stru);
}

热点排行