package string; import java.nio.charset.Charset; public class UTF82GBK { public static void main(String[] args) throws Exception { //系统的默认编码是GBK System.out.println("Default Charset=" + Charset.defaultCharset()); String t = "hfjkds中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国hfsdkj<img src='sasa' /> fjldsajflkdsjaflkdsjalf <img src='sada' ait=''/>sfdsfadas"; //思路:先转为Unicode,然后转为GBK String utf8 = new String(t.getBytes( "UTF-8")); //等同于: // String utf8 = new String(t.getBytes( "UTF-8"),Charset.defaultCharset()); System.out.println(utf8); String unicode = new String(utf8.getBytes(),"UTF-8"); //等同于: // String unicode = new String(utf8.getBytes(Charset.defaultCharset()),"UTF-8"); System.out.println(unicode); String gbk = new String(unicode.getBytes("GBK")); //等同于: // String gbk = new String(unicode.getBytes("GBK"),Charset.defaultCharset()); System.out.println(gbk); } }
byte[] bytes = s.getBytes(); for (byte aByte : bytes) { System.out.println("byte十进制:" + (256 + (int)aByte) + ";16进制:" + String.format("%08x", (256 + (int)aByte))); } System.out.println("------------------------"); byte[] gbks = s.getBytes("GBK"); for (byte aByte : gbks) { System.out.println("GBK-byte十进制 :" + (256 + (int)aByte) + ";16进制:" + String.format("%08x", (256 + (int)aByte))); } ------------ byte十进制:228;16进制:000000e4 byte十进制:184;16进制:000000b8 byte十进制:141;16进制:0000008d ------------------------ GBK-byte十进制 :178;16进制:000000b2 GBK-byte十进制 :187;16进制:000000bb