char <-->unicode

http://daoshud1.iteye.com/blog/1879315

/**
	 * 字符转Unicode
	 * 
	 * @param s
	 * @return
	 */
	public static String gbEncoding(final String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			str += "\\u" + Integer.toHexString(ch);
		}
		return str;
	}

	/**
	 * Unicode转字符
	 * 
	 * @param str
	 * @return
	 */
	public static String encodingtoStr(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;
	}

猜你喜欢

转载自snowelf.iteye.com/blog/2173149
今日推荐