16进制Unicode码和汉字互相转换
后台给我返回了一堆这样的东西,咱也不敢问,只能自己解决了!
上代码:
function unicode16ToCn(unicode16data){
console.log(unicode16data);
var trans = unicode16data.match(/\\u[0-9a-fA-F]{4}/g);
if(trans == null) {
return unicode16data;
}
for(var i = 0; i < trans.length; i++){
unicode16data = unicode16data.replace(trans[i],unescape(trans[i].replace("\\u","%u")));
}
return unicode16data;
}
嗯,完美解决了
那么需要转回Unicode16时呢,代码如下:
function cnToUnicode16(cn){ var ascii = ""; for (var i = 0; i < cn.length; i++) { var code = Number(cn[i].charCodeAt(0)); if (!$("onlyzw").checked || code > 127) { var charAscii = code.toString(16); charAscii = new String("0000").substring(charAscii.length, 4) + charAscii; ascii += "\\u" + charAscii; } else { ascii += cn[i]; } } return ascii; }