Java字符编码转换

// GBK转UTF-8
public String gbkToUtf8(String str) throws UnsupportedEncodingException {
    String iso=new String(str.getBytes("UTF-8"), "ISO-8859-1");
    for(byte b:iso.getBytes("ISO-8859-1")){}
    return new String(iso.getBytes("ISO-8859-1"),"UTF-8");
}
// UTF-8转GBK public String utf8ToGbk(String str) throws UnsupportedEncodingException { String iso=new String(str.getBytes("GB2312"), "ISO-8859-1"); for(byte b:iso.getBytes("ISO-8859-1")){} return new String(iso.getBytes("ISO-8859-1"),"GB2312"); }

 

posted @ 2021-12-13 10:33  散人长情  阅读(234)  评论(0编辑  收藏  举报