ub = new String(s.getBytes("utf-8")).getBytes();
->
ub = new String(s.getBytes()).getBytes("utf-8");

解决方案 »

  1.   

    getBytes()是编码为系统默认字符集
      

  2.   

    ub = new String(s.getBytes("utf-8")).getBytes();和
    ub = new String(s.getBytes()).getBytes("utf-8");
    分别的含义是什么?为什么第一个的结果不正确?
      

  3.   

    import java.io.UnsupportedEncodingException;
    public class test
    {
    public static void main(String[] args)
    {
    String s;
    try
    {
    s = "最后";
                               s=new String(s.getBytes("ISO-8859-1"),"utf-8");  //加上这个就ok了
    byte[] ub = null; ub = s.getBytes("utf-8");
    for (int i = 0; i < ub.length; i++)
    {
    System.out.print("(" + ub[i] + ")");
    }
    System.out.println(); ub = new String(s.getBytes("utf-8")).getBytes();
    for (int i = 0; i < ub.length; i++)
    {
    System.out.print("(" + ub[i] + ")");
    }
    System.out.println();
    }
    catch (UnsupportedEncodingException e1)
    {
    }
    }
    }
      

  4.   

    alaal(穷街) 你的代码运行结果是什么?我运行的全是问号!shaopin(shaopin)能讲讲你的代码和我写得有什么不一样?
      

  5.   

    我的正常
    ub = new String(s.getBytes("utf-8")).getBytes();
    以为utf-8为字符集构造新的string,然后用系统默认字符集转为byteub = new String(s.getBytes()).getBytes("utf-8");
    用系统默认字符集造新的string,然后用utf-8字符集转为byte
      

  6.   

    ub = new String(s.getBytes("utf-8")).getBytes();
    以为utf-8为字符集构造新的string,然后用系统默认字符集转为byteub = new String(s.getBytes()).getBytes("utf-8");
    用系统默认字符集造新的string,然后用utf-8字符集转为byte
    ------------------------------------------------
     alaal(穷街)说得对