Unicode转中文,Unicode编码转换,ASCII转Unicode,Unicode转ASCII

2019-08-21 16:54:42 浏览数 (1)

DEMO https://oktools.net/unicode

Unicode转中文

代码语言:txt复制
   function decodeUnicode() {
        let input = area_input.value;
        area_output.value = unescape(input.replace(/\u/gi, '%u'));
        
    }

Unicode编码

代码语言:txt复制
    function encodeUnicode() {
        let input = area_input.value;
        let res = [];
        for (let i = 0; i < input.length; i  ) {
            res[i] = ("00"   input.charCodeAt(i).toString(16)).slice(-4);
        }
        area_output.value = "\u"   res.join("\u");
    
    }

ASCII转Unicode

代码语言:txt复制
   function asciiToUnicode() {
        let input = area_input.value;
        let code = input.match(/&#(d );/g);
        let result = '';
        if (code === null) {
            area_output.value = '';
            return
        }
        for (let i = 0; i < code.length; i  ) {
            result  = String.fromCharCode(code[i].replace(/[&#;]/g, ''));
        }
        area_output.value = result;
      
    }

Unicode转ASCII

代码语言:txt复制
    function unicodeToAscii() {
        let input = area_input.value;
        let result = '';
        for (let i = 0; i < input.length; i  ) {
            result  = '&#'   input.charCodeAt(i)   ';';
        }
        area_output.value = result;
      
    }

效果

image.pngimage.png

0 人点赞