中文与 Unicode 编码的相互转换

 

 Unicode 16 进制编码转换为 中文字符串

 public static String UnicodeToChar(String unicodeString){ StringBuffer charString = new StringBuffer(); for (int i = 0; i + 4 <= unicodeString.length(); i = i + 4) { int j = Integer.parseInt(unicodeString.substring(i, i + 4), 16); charString.append((char) j); } return charString.toString(); }

 

 中文字符串 转换为 Unicode 16 进制编码

 public static String CharToUnicode(String s) { try { StringBuffer out = new StringBuffer(""); byte[] bytes = s.getBytes("unicode"); for (int i = 2; i < bytes.length - 1; i += 2) { String str = Integer.toHexString(bytes[i + 1] & 0xff); for (int j = str.length(); j < 2; j++) { out.append("0"); } String str1 = Integer.toHexString(bytes[i] & 0xff); out.append(str); out.append(str1); } return out.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } }

你可能感兴趣的:(J2SE)