java unicode转中文
public static String unicodeToString(String str) { Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); Matcher matcher = pattern.matcher(str); char ch; while (matcher.find()) { ch = (char) Integer.parseInt(matcher.group(2), 16); str = str.replace(matcher.group(1), ch + ""); } return str; }
获取字符串的unicode编码,这个我们可以通过直接获取字符串的unicode二进制,然后将其byte转换成对应的16进制表示
static String getUnicode(String s) { try { StringBuffer out = new StringBuffer(""); byte[] bytes = s.getBytes("unicode"); for (int i = 0; i < bytes.length - 1; i += 2) { out.append("\\u"); 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(str1); out.append(str); } return out.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } }