计算精度问题带根号√? ? ??3√
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
}
}