UnicodeUtil.java

发布时间:2023年12月18日

计算精度问题带根号√? ? ??3√

UNICODE字符表

package util;

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

/**
 * radical
 * 根号√运算问题  Unicode 
 * 
 * @author ZengWenFeng
 * @email 117791303@qq.com
 * @date 2015.04.07
 */
public class UnicodeUtil
{
	/**
	 * 字符串转Unicode
	 * 
	 * @author ZengWenFeng
	 * @email 117791303@qq.com
	 * @date 2015.04.07
	 * @param string
	 * @return
	 */
	public static String encode(String string)
	{
		char[] utfBytes = string.toCharArray();
		String unicodeBytes = "";
		for (int i = 0; i < utfBytes.length; i++)
		{
			String hexB = Integer.toHexString(utfBytes[i]);
			if (hexB.length() <= 2)
			{
				hexB = "00" + hexB;
			}
			unicodeBytes = unicodeBytes + "\\u" + hexB;
		}
		return unicodeBytes;
	}

	/**
	 * Unicode转字符串
	 * 
	 * @author ZengWenFeng
	 * @email 117791303@qq.com
	 * @date 2015.04.07
	 * @param unicode
	 * @return
	 */
	public static String decode(String unicode)
	{
		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
		Matcher matcher = pattern.matcher(unicode);
		char ch;
		
		while (matcher.find())
		{
			ch = (char) Integer.parseInt(matcher.group(2), 16);
			unicode = unicode.replace(matcher.group(1), ch + "");
		}
		
		return unicode;
	}

	public static void main(String[] args)
	{
		String str = "文锋";

		
		String unicode = encode(str);
		System.out.println(str + " ---> " + unicode);  //      \u6587\u950b

		
		String zh_cn = decode(unicode);
		System.out.println(unicode + " ---> " + zh_cn);

		
		zh_cn = decode("\u2713");
		System.out.println(zh_cn);
		
		//3√
		unicode = encode("33√");
		System.out.println(unicode);//  \u0033\u00b3\u221a
	}

}

文章来源:https://blog.csdn.net/spencer_tseng/article/details/135010008
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。