日期:2014-05-18  浏览次数:20765 次

GBK转UTF-8乱码
String desc="CSS测试";
try {
desc = new String(desc.getBytes(),"gbk");
System.out.println(desc); //输出为:CSS测试
desc=new String (desc.getBytes("gbk"),"UTF-8");
System.out.println(desc); //这边却输入为CSS????
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

哪位高人帮忙看看,GBK转成UTF-8后就乱码了???

------解决方案--------------------
这是读取的机器默认的编码,中文的机器就是gbk,所以这个字符串本身就是gbk的,你转成utf-8会乱码
------解决方案--------------------
仔细看看api,就可以搞定。desc=new String (desc.getBytes("utf8"),"UTF-8"); 
这样写

Java code

    public static void main(String[] args) {
        String desc="CSS测试"; 
        try { 
            desc = new String(desc.getBytes(),"gbk"); 
            System.out.println(desc);                                          //输出为:CSS测试 
            desc=new String (desc.getBytes("utf8"),"UTF-8"); 
            System.out.println(desc);                                          //这边却输入为CSS???? 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 

    }