日期:2014-05-20  浏览次数:20803 次

还是编码问题
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);
}