Unicode转中文
function decodeUnicode(str) {
return unescape(str.replace(/\\u/gi, '%u'))
}
中文转Unicode
function encodeUnicode(str) {
let res = [];
for (let i = 0; i < str.length; i++) {
res[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
}
return "\\u" + res.join("\\u");
}
Ascii 转 Unicode
function A2U(str) {
var reserved = '';
for (var i = 0; i < str.length; i++) {
reserved += '&#' + str.charCodeAt(i) + ';';
}
return reserved;
}
Unicode 转 Ascii
function U2A(str) {
var reserved = '';
var code = str.match(/&#(d+);/g);
if (code === null) {
return str;
}
for (var i = 0; i < code.length; i++) {
reserved += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
}
return reserved;
}