# js编码解码

# escape 和 unescape


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

escape(string)
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

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 进行解码。

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

# encodeURIComponent 和 decodeURIComponent


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

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

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

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

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

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 (opens new window)
https://kylethanas.coding.me/demo/js/sha1.js (opens new window)
var sha = hex_sha1(navigator.userAgent)
md5 (opens new window)
http://kylethanas.coding.me/demo/js/md5.js (opens new window)
var hash = hex_md5(navigator.userAgent);

我是Kylethanas🐏