JS Unicode转中文,中文转Unicode,ASCII转Unicode,Unicode转ASCII

在线转换工具https://oktools.net/unicode

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;
}

  

猜你喜欢

转载自www.cnblogs.com/vivec/p/11300941.html