java中GB2312 To Utf-8字符转换怎么转呀!谁能告诉我!谢谢了!

解决方案 »

  1.   

    new String(str.getBytes("GB2312"),"UTF-8");   
      

  2.   

    import java.io.UnsupportedEncodingException;public class Test01 {    /**
         * @param args
         * @throws UnsupportedEncodingException
         *
         */
        public static void main(String[] args) throws UnsupportedEncodingException {        String str1 = "中";
        
            byte[] b1 = str1.getBytes("UTF-8");
            for(int i=0;i<b1.length;i++){
                System.out.println("<<<<"+Integer.toHexString(b1[i]&0xff));//得到str1对应的UTF-8编码数组。
            }
        }}
      

  3.   

    public String encode(String value ,String type){
    Charset charSet = Charset.forName(type);   
        ByteBuffer buffer = charSet.encode(value);            
        value = new String(buffer.array()).trim();
        return value;
    }
    value 是原字符串,type是编码方式,比如"UTF-8"