汉字->6C49 5B57
public static void main(String[] args) {
System.out.println(charsetEncode(汉字));
System.out.println(charsetDeCode(charsetEncode(汉字)));
}
private String charsetEncode(String str) {
if ("".equals(str)) {
return "";
}
StringBuffer strBuff = new StringBuffer("");
try {
byte b[] = str.getBytes("UTF-16");
for (int n = 0; n < b.length; n++) {
str = (Integer.toHexString(b[n] & 0XFF));
if (str.length() == 1) {
strBuff.append("0").append(str);
} else {
strBuff.append(str);
}
}
// 去除第一个标记字符
str = strBuff.toString().toUpperCase().substring(4);
// System.out.println(str);
char[] chs = str.toCharArray();
strBuff.delete(0, strBuff.length());
for (int i = 0; i < chs.length; i = i + 4) {
strBuff.append(chs[i])
.append(chs[i + 1])
.append(chs[i + 2])
.append(chs[i + 3])
.append(" ");
}
} catch (Exception e) {
System.out.print(e.getStackTrace());
e.getStackTrace();
}
return strBuff.toString();
}
private String charsetDeCode(String str) {
if ("".equals(str)) {
return "";
}
StringBuffer sb = new StringBuffer("");
str = str.replace(" ", "");
try {
for (int i =0; i < str.length()-3; i = i + 4){
sb.append((char)Integer.valueOf(str.substring(i, i+4),16).intValue());
}
} catch (Exception e) {
System.out.print(e.getStackTrace());
e.getStackTrace();
}
return sb.toString();
}
String str= "日本語";
char c=(char)str.charAt(0);
System.out.println(c+ "===》: "+Integer.toString(c,16));
System.out.println( "====: "+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue());