当前位置: 代码迷 >> Java Web开发 >> GBK转UTF-8乱码解决思路
  详细解决方案

GBK转UTF-8乱码解决思路

热度:116   发布时间:2016-04-17 01:15:12.0
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();         }     }
  相关解决方案