java
package mydemo;
/**
* <p>unicode 编码解码工具类<p>
* @author xieh
*
*/
public class UnicodeUtil {
/**
* <p>转为unicode 编码<p>
* @param string
* @return unicodeString
*/
public static String encode(String str) {
String prifix = "\\u";
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
String code = prifix + format(Integer.toHexString(c));
unicode.append(code);
}
return unicode.toString();
}
/**
* <p>unicode 解码<p>
* @param unicode
* @return originalString
*/
public static String decode(String unicode) {
StringBuffer str = new StringBuffer();
String[] hex = unicode.split("\\\\u");
for (int i = 1; i < hex.length; i++) {
int data = Integer.parseInt(hex[i], 16);
str.append((char) data);
}
return str.length() > 0 ? str.toString() : unicode;
}
/**
* <p>转为数据库查询编码<p>
* 向数据库查询时,\\u需要转为%
* @param str
* @return
*/
public static String encodeDBSearchParam(String str) {
str = encode(str);
str = str.replace("\\", "%");
return str;
}
/**
* <p>解码数据库查询参数<p>
* 数据库查询后,回传参数% 转回\\u
* @param str
* @return
*/
public static String decodeDBSearchParam(String str) {
str = str.replace("%", "\\");
str = decode(str);
return str;
}
/**
* 为长度不足4位的unicode 值补零
* @param str
* @return
*/
private static String format(String str) {
for ( int i=0, l=4-str.length(); i<l; i++ )
str = "0" + str;
return str;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
String string = "澳大利亚";
String encode = encode(string);
System.out.println(encode);
String decode = decode(encode);
System.out.println(decode);
}
}
JS
<script type="text/javascript">
// 转为unicode 编码
function encodeUnicode(str) {
var res = [];
for (var i = 0; i < str.length; i++) {
res[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
}
return "\\u" + res.join("\\u");
}
// 解码
function decodeUnicode(str) {
str = str.replace(/\\/g, "%");
//转换中文
str = unescape(str);
//将其他受影响的转换回原来
str = str.replace(/%/g, "\\");
//对网址的链接进行处理
str = str.replace(/\\/g, "");
return str;
}
var a = encodeUnicode("澳大利亚");
var b = decodeUnicode(a);
</script>