import java.io.UnsupportedEncodingException;
public class ziFuChuan{
public static void main(String arg[]){
String hello = "Hello world 世界你好";
try{
      hello = new String(hello.getBytes(), "GB2312");
      byte[] b=hello.getBytes("gb2312");
      System.out.println(b.length);//输出20      hello = new String(hello.getBytes(), "UTF-8");
System.out.println(hello.length());//输出18 byte[] b1=hello.getBytes("UTF-8");
System.out.println(b1.length);//输出28
System.out.println(hello.length());//输出18    }
    catch(UnsupportedEncodingException e){}
}
}

解决方案 »

  1.   

    这个是源程序,还是搞不懂怎么得出来的
    import java.io.UnsupportedEncodingException;
    public class ZiFuChuan{
    public static void main(String arg[]){
    String hello = "Hello world 世界你好";
    try{
          //hello = new String(hello.getBytes(), "GB2312");
          byte[] b=hello.getBytes("gb2312");
          System.out.println(b.length);//输出20      String hello1 = new String(hello.getBytes(), "gb2312");
      System.out.println(hello1.length());//输出16
    //hello=new String(hello.getBytes(),"UTF-8");//----(1)
    byte[] b1=hello.getBytes("UTF-8");
    System.out.println(b1.length);//输出24,如果加上(1)输出28
    String hello2=new String(hello.getBytes(),"UTF-8");
    System.out.println(hello2.length());//输出18    }
        catch(UnsupportedEncodingException e){}
    }
    }