js编码解码

目录

  1. escape 和 unescape
  2. encodeURI 和 decodeURI
  3. encodeURIComponent 和 decodeURIComponent
  4. js中的BASE64算法
  5. 更多编码、算法

escape 和 unescape


原理:对除ASCII字母、数字、标点符号 @ * _ + - . / 之外的其余字符进行编码javascript

escape(string) escape() 函数可对字符串进行编码,这样就能够在全部的计算机上读取该字符串。java

escape("Kyle Thanas!测试")
"Kyle%20Thanas%21%u6D4B%u8BD5"
复制代码

unescape(string) unescape() 函数可对经过 escape() 编码的字符串进行解码。算法

unescape("Kyle%20Thanas%21%u6D4B%u8BD5")
"Kyle Thanas!测试"
复制代码

encodeURI 和 decodeURI


原理:返回编码为有效的统一资源标识符 (URI) 的字符串,不会被编码的字符:! @ # $ & * ( ) = : / ; ? + '函数

encodeURI(URIstring) encodeURI() 函数可把字符串做为 URI 进行编码。测试

encodeURI("https://www.baidu.com/s?wd=编码")
"https://www.baidu.com/s?wd=%E7%BC%96%E7%A0%81"
复制代码

decodeURI(URIstring) decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。ui

decodeURI("https://www.baidu.com/s?wd=%E7%BC%96%E7%A0%81")
"https://www.baidu.com/s?wd=编码"
复制代码

encodeURIComponent 和 decodeURIComponent


原理:对URL的组成部分进行个别编码,而不用于对整个URL进行编码this

encodeURIComponent(URIstring) encodeURIComponent() 函数可把字符串做为 URI 组件进行编码。编码

encodeURIComponent("https://coding.net/u/KyleThanas")
"https%3A%2F%2Fcoding.net%2Fu%2FKyleThanas"
复制代码

decodeURIComponent(URIstring) decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。spa

decodeURIComponent("https%3A%2F%2Fcoding.net%2Fu%2FKyleThanas")
"https://coding.net/u/KyleThanas"
复制代码

js中的BASE64算法


var Base64 = {
        table: [
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '+', '/'
        ],
        UTF16ToUTF8: function (str) {
            var res = [],
                len = str.length;
            for (var i = 0; i < len; i++) {
                var code = str.charCodeAt(i);
                if (code > 0x0000 && code <= 0x007F) {
                    res.push(str.charAt(i));
                } else if (code >= 0x0080 && code <= 0x07FF) {
                    var byte1 = 0xC0 | ((code >> 6) & 0x1F);
                    var byte2 = 0x80 | (code & 0x3F);
                    res.push(
                        String.fromCharCode(byte1),
                        String.fromCharCode(byte2)
                    );
                } else if (code >= 0x0800 && code <= 0xFFFF) {
                    var byte1 = 0xE0 | ((code >> 12) & 0x0F);
                    var byte2 = 0x80 | ((code >> 6) & 0x3F);
                    var byte3 = 0x80 | (code & 0x3F);
                    res.push(
                        String.fromCharCode(byte1),
                        String.fromCharCode(byte2),
                        String.fromCharCode(byte3)
                    );
                } else {}
            }
            return res.join('');
        },
        UTF8ToUTF16: function (str) {
            var res = [],
                len = str.length;
            var i = 0;
            for (var i = 0; i < len; i++) {
                var code = str.charCodeAt(i);
                if (((code >> 7) & 0xFF) == 0x0) {
                    res.push(str.charAt(i));
                } else if (((code >> 5) & 0xFF) == 0x6) {
                    var code2 = str.charCodeAt(++i);
                    var byte1 = (code & 0x1F) << 6;
                    var byte2 = code2 & 0x3F;
                    var utf16 = byte1 | byte2;
                    res.push(Sting.fromCharCode(utf16));
                } else if (((code >> 4) & 0xFF) == 0xE) {
                    var code2 = str.charCodeAt(++i);
                    var code3 = str.charCodeAt(++i);
                    var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
                    var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
                    utf16 = ((byte1 & 0x00FF) << 8) | byte2
                    res.push(String.fromCharCode(utf16));
                } else {}
            }
            return res.join('');
        },
        encode: function (str) {
            !str ? '' : 1;
            var utf8 = this.UTF16ToUTF8(str),
                i = 0,
                res = [];
            var len = utf8.length;
            while (i < len) {
                var c1 = utf8.charCodeAt(i++) & 0xFF;
                res.push(this.table[c1 >> 2]);
                if (i == len) {
                    res.push(this.table[(c1 & 0x3) << 4]);
                    res.push('==');
                    break;
                }
                var c2 = utf8.charCodeAt(i++);
                if (i == len) {
                    res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
                    res.push(this.table[(c2 & 0x0F) << 2]);
                    res.push('=');
                    break;
                }
                var c3 = utf8.charCodeAt(i++);
                res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
                res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
                res.push(this.table[c3 & 0x3F]);
            }
            return res.join('');
        },
        decode: function (str) {
            !str ? '' : 1;
            var len = str.length,
                i = 0,
                res = [];
            while (i < len) {
                code1 = this.table.indexOf(str.charAt(i++));
                code2 = this.table.indexOf(str.charAt(i++));
                code3 = this.table.indexOf(str.charAt(i++));
                code4 = this.table.indexOf(str.charAt(i++));
                c1 = (code1 << 2) | (code2 >> 4);
                c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
                c3 = ((code3 & 0x3) << 6) | code4;
                res.push(String.fromCharCode(c1));
                code3 != 64 ? res.push(String.fromCharCode(c2)) : 1;
                code4 != 64 ? res.push(String.fromCharCode(c3)) : 1;
            }
            return this.UTF8ToUTF16(res.join(''));
        }
    };
    var b64 = Base64.encode('KyleThanas,Base64,简书');
    console.log(b64);
    console.log(Base64.decode(b64));
复制代码

其余的转换

JSON.parse(escape2Html(data));.net

function escape2Html(str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
      return arrEntities[t];
    });
 }
复制代码

更多编码、算法

sha1 https://kylethanas.coding.me/demo/js/sha1.js var sha = hex_sha1(navigator.userAgent) md5 http://kylethanas.coding.me/demo/js/md5.js var hash = hex_md5(navigator.userAgent);

我是Kylethanas🐏