unicode码对每一个字符用4位16进制数表示。具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数, 如果转化的16进制数的长度不足2位,则在其后补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\u" 即可。
方法一:转换原理代码实现
/** * 将字符串转成unicode * @param str 待转字符串 * @return unicode字符串 */ public String convert(String str) { str = (str == null ? "" : str); String tmp; StringBuffer sb = new StringBuffer(1000); char c; int i, j; (0); for (i = 0; i < (); i++) { c = (i); ("\\u"); j = (c >>>8); //取出高8位 tmp = Integer.toHexString(j); if (() == 1) ("0"); (tmp); j = (c & 0xFF); //取出低8位 tmp = Integer.toHexString(j); if (() == 1) ("0"); (tmp); } return (new String(sb)); } /** * 将unicode 字符串 * @param str 待转字符串 * @return 普通字符串 */ public String revert(String str) { str = (str == null ? "" : str); if (("\\u") == -1)//如果不是unicode码则原样返回 return str; StringBuffer sb = new StringBuffer(1000); for (int i = 0; i <=() - 6;) { String strTemp = (i, i + 6); String value = (2); int c = 0; for (int j = 0; j < (); j++) { char tempChar = (j); int t = 0; switch (tempChar) { case 'a': t = 10; break; case 'b': t = 11; break; case 'c': t = 12; break; case 'd': t = 13; break; case 'e': t = 14; break; case 'f': t = 15; break; default: t = tempChar - 48; break; } c += t * ((int) Math.pow(16, (() - j - 1))); } ((char) c); i = i + 6; } return (); }方法二:api实现
/** * 字符串转换unicode */ public static String convert(String string) { StringBuffer unicode = new StringBuffer(); for (int i = 0; i < (); i++) { // 取出每一个字符 char c = (i); // 转换为unicode (String.format("\\u%04x",Integer.valueOf(c))); } return (); } /** * unicode 转字符串 */ public static String revert(String unicode) { StringBuffer string = new StringBuffer(); String[] hex = ("\\\\u"); for (int i = 1; i < hex.length; i++) { // 转换出每一个代码点 int data = Integer.parseInt(hex[i], 16); // 追加成string ((char) data); } return (); }