JAVASCRIPT BASE64 encoding and decoding

Source: Internet
Author: User
Tags 0xc0 base64

  • Code:
    var Base64 = {//transcoding tables 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) {//single byte, this is not considered 0x0000, because it is empty bytes//u+00000000–u+0        000007F 0xxxxxxx Res.push (Str.charat (i)); } else if (code >= 0x0080 && Code <= 0x07ff) {//Double byte//u+00000080–u+000007ff 110x XXXX 10xxxxxx//110xxxxx var byte1 = 0xC0 |            (Code >> 6) & 0x1F); 10xxxxxx var byte2 = 0x80 |            (Code & 0x3F);        Res.push (String.fromCharCode (byte1), String.fromCharCode (Byte2)); } else if (code >= 0x0800 && Code <= 0xFFFF) {//three bytes//U+00000800–U+0000FFFF 1110 XXXX 10xxxxxx 10xxxxxx//1110xxxx var byte1 = 0xE0 |            (Code >>) & 0x0F); 10xxxxxx var byte2 = 0x80 |            (Code >> 6) & 0x3F); 10xxxxxx var byte3 = 0x80 |            (Code & 0x3F); Res.push (String.fromCharCode (byte1), String.fromCharCode (Byte2), string.fr        Omcharcode (Byte3)); } else if (code >= 0x00010000 && Code <= 0X001FFFFF) {//four bytes//U+00010000–U+001FFF            FF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx} else if (code >= 0x00200000 && Code <= 0x03ffffff) { Five Bytes//u+00200000–U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx} else/** if (code >= 0x04000000 && Code <= 0x7FFFFFFF) */{//Six bytes//u+04000000–u+7fffffff 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10x    XXXXX}} 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); The first byte is judged if (((Code >> 7) & 0xFF) = = 0x0) {//single-byte//0xxxxxxx Res.        Push (Str.charat (i));  } else if (((Code >> 5) & 0xFF) = = 0x6) {//double-byte//110xxxxx 10xxxxxx var code2            = Str.charcodeat (++i);            var byte1 = (Code & 0x1F) << 6;            var byte2 = code2 & 0x3F; var utf16 = byte1 |            Byte2;        Res.push (String.fromCharCode (UTF16)); } else if (((Code >> 4) & 0xFF) = = 0xE) {//Three bytes//1110xxxx 10xxxxxx 10xxxxxx var code2 = str.charcodeat (++i);            var code3 = str.charcodeat (++i); var byte1 = (Code << 4) |            ((Code2 >> 2) & 0x0F); var byte2 = ((Code2 & 0x03) << 6) |            (Code3 & 0x3F); var utf16 = ((Byte1 & 0x00FF) << 8) |        Byte2 Res.push (String.fromCharCode (UTF16));        } else if (((Code >> 3) & 0xFF) = = 0x1E) {//four bytes//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (((Code >> 2) & 0xFF) = = 0x3e) {//Five bytes//111110xx 10xxxxxx 10xxxxxx 10xx XXXX 10xxxxxx} else/** if (((Code >> 1) & 0xFF) = = 0x7E) */{//Six bytes//1111110x 1         0xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx}} return Res.join (");},encode:function (str) {if (!STR) {    Return '; } var UTF8 = this. Utf16toutf8 (str); Turn into UTF8 var i = 0; Traverse index var len = utf8.length;    var res = [];        while (I < len) {var C1 = utf8.charcodeat (i++) & 0xFF;        Res.push (THIS.TABLE[C1 >> 2]);            Need to fill 2 = if (i = = len) {Res.push (this.table[(C1 & 0x3) << 4]);            Res.push (' = = ');        Break        } var c2 = Utf8.charcodeat (i++); Need to fill 1 = 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) {if (!str) {return ';    } var len = str.length;    var i = 0; var 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);        Res.push (String.fromCharCode (C1)); if (code3! =-1) {C2 = ((Code2 & 0xF) << 4) |            (Code3 >> 2);        Res.push (String.fromCharCode (C2));            } if (Code4! =-1) {C3 = ((Code3 & 0x3) << 6) | Code4;        Res.push (String.fromCharCode (C3)); }} return this. Utf8toutf16 (Res.join ('));}};
  • Example:
    var a = Base64.encode(‘小火柴‘)console.log(a)  #  输出  5bCP54Gr5p+0var b = Base64.decode(a)  console.log(b)  #  输出  小火柴
  • JAVASCRIPT BASE64 encoding and decoding

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.