= new String(msg.getBytes(utf-8), "GB2312");

解决方案 »

  1.   

    public class Test{
      public static void main(String[] args) throws Exception{
        String s = "用java如何实现gb2312和utf-8之间的转换";    
        byte[] gb2312 = s.getBytes("GB2312");
        for(int i=0;i<gb2312.length;i++)
          System.out.print(gb2312[i]+",");
        System.out.println();
        s = new String(gb2312,"GB2312");    
        byte[] utf8 = s.getBytes("UTF-8");
        for(int i=0;i<utf8.length;i++)
          System.out.print(utf8[i]+",");    
        System.out.println("\n"+new String(utf8,"UTF-8"));    
      }
    }
      

  2.   

    搂主,送你连个有用的函数(要先import java.io.IOException;): public String toUTF(String str) throws IOException
    {
    str = new String(str.getBytes("gb2312"),"ISO-8859-1");
    return str;
    }         public String toGB(String str) throws IOException
    {
    str = new String(str.getBytes("ISO-8859-1"),"gb2312");
    return str;
    }
      

  3.   

    使用方法:
    GB to UTF:String gbstr="abc";
    String utfstr=toUTF(gbstr);UTF to GB:String utfstr="abc";
    String gbstr=toGB(utfstr);
      

  4.   

    送你一个转换类:
    public class ExChinese {
      public ExChinese() {
      }
      public String ex_ch(String str)
      {
        if(str==null)
        {
          str  ="";
        }
        else
        {
          try
          {
            str = new String(str.getBytes("iso-8859-1"),"gb2312");
          }
          catch (Exception ex)
          {
            ex.printStackTrace();
          }
        }
      return str ;
     }
    }