`
umbrellall1
  • 浏览: 142275 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

(小工具)将汉字转换为uincode码和uincode转换为汉字

 
阅读更多
package com.convert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConvertCode {
	
	
	/**
	 * 将汉字转换为Unicode 码
	 * */
	 public static String toUnicode(String s) {
		 String as[] = new String[s.length()];
		 String s1 = "";
		 for (int i = 0; i < s.length(); i++) {
			as[i] = Integer.toHexString(s.charAt(i) & 0xffff);
			s1 = s1 + "\\u" + as[i];
		 }
		 return s1;
	}

	 /**
		 * 将 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;
	}

	
	public static void main(String[] args) {
		System.out.println(toUnicode("程序员"));
		System.out.println(UnicodeToString("\u7a0b\u5e8f\u5458"));
	}
}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics