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,那么结果应该是

鑻?
英但为什么实际上运行时是

鑻?
???????????

解决方案 »

  1.   

    你干脆这样就全部是中文的了.编码的问题蛮复杂的,一下很难说清,慢慢领悟吧.
    import java.io.UnsupportedEncodingException;class test{
    public static void main(String[] args) throws UnsupportedEncodingException {
    String str = "英";
    byte utf[] = str.getBytes("GBK");
    String strUTF = new String(utf);
    byte[] u = strUTF.getBytes("UTF-8");
    String stru = new String(u,"UTF-8");
    System.out.println(str);
    System.out.println(strUTF);
    System.out.println(stru);
    }
    }
      

  2.   

    换言之,GBK   GB2312   UTF-8  iso-8859-1 这几种编码不能任意转换的?
    通过str.getBytes()方法必须按照一定规则,那么这个规则是什么?
    等......
      

  3.   

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