java 编码转换
package test; import java.util.Map; public class Test3 { public static Map<String,Object> map; public static void main(String[] args) throws Exception { String str = "严"; // "GBK","UTF-8" byte[] bytes = str.getBytes("UTF-8"); printHexString(bytes); System.out.println(); String str2 = new String(bytes,"UTF-8"); System.out.println(str2); } //将指定byte数组以16进制的形式打印到控制台 public static void printHexString( byte[] b) { for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } System.out.print(hex.toUpperCase() ); } } }
相关函数
byte[] bytes = str.getBytes("UTF-8");
以指定的字符集对字符串进行编码,结果存放在byte数组中
String str2 = new String(bytes,"UTF-8");
以指定的字符集对给定的byte数组进行解码
"严"的UTF编码为:E4B8A5,GBK编码为:D1CF