Use of Base64 and gzip in JavaScript

Source: Internet
Author: User
Tags 0xc0 base64

Transcoding and decoding of base64

/** * UTF16 and UTF8 conversion table * u+00000000–u+0000007f 0xxxxxxx* u+00000080–u+000007ff 110xxxxx 10xxxxxx* u+00000800–u+00 00FFFF 1110xxxx 10xxxxxx 10xxxxxx* u+00010000–u+001fffff 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx* u+00200000–u+03fffff F 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx* u+04000000–u+7fffffff 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 1 0xxxxxx*/varBase64 = {    //transcoding Tablestable: [' 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) {varres = [], Len =str.length;  for(vari = 0; i < Len; i++) {            varCode =str.charcodeat (i); if(Code > 0x0000 && Code <= 0x007F) {                //single Byte, here is not considered 0x0000, because it is empty byte                //u+00000000–u+0000007f 0xxxxxxxRes.push (Str.charat (i)); } Else if(Code >= 0x0080 && code <= 0x07ff) {                //Double byte                //u+00000080–u+000007ff 110xxxxx 10xxxxxx                //110xxxxx                varByte1 = 0xC0 | (Code >> 6) & 0x1F); //10xxxxxx                varByte2 = 0x80 | (Code & 0x3F);            Res.push (String.fromCharCode (byte1), String.fromCharCode (Byte2)); } Else if(Code >= 0x0800 && code <= 0xFFFF) {                //three bytes                //u+00000800–u+0000ffff 1110xxxx 10xxxxxx 10xxxxxx                //1110xxxx                varByte1 = 0xE0 | (Code >>) & 0x0F); //10xxxxxx                varByte2 = 0x80 | (Code >> 6) & 0x3F); //10xxxxxx                varByte3 = 0x80 | (Code & 0x3F);                     Res.push (String.fromCharCode (byte1), String.fromCharCode (Byte2),            String.fromCharCode (Byte3)); } Else if(Code >= 0x00010000 && code <= 0X001FFFFF) {                //Four bytes                //u+00010000–u+001fffff 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 10xxxxxx            }        }         returnRes.join (' '); }, Utf8toutf16:function(str) {varres = [], Len =str.length; vari = 0;  for(vari = 0; i < Len; i++) {            varCode =str.charcodeat (i); //make a judgment on the first byte            if((Code >> 7) & 0xFF) = = 0x0) {                //Single byte                //0xxxxxxxRes.push (Str.charat (i)); } Else if((Code >> 5) & 0xFF) = = 0x6) {                //Double byte                //110xxxxx 10xxxxxx                varCode2 = str.charcodeat (+ +i); varByte1 = (Code & 0x1F) << 6; varByte2 = Code2 & 0x3F; varUTF16 = Byte1 |Byte2;            Res.push (Sting.fromcharcode (UTF16)); } Else if((Code >> 4) & 0xFF) = = 0xE) {                //three bytes                //1110xxxx 10xxxxxx 10xxxxxx                varCode2 = str.charcodeat (+ +i); varCode3 = str.charcodeat (+ +i); varbyte1 = (Code << 4) | ((Code2 >> 2) & 0x0F); varByte2 = ((Code2 & 0x03) << 6) | (Code3 & 0x3F); varUTF16 = ((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 10xxxxxx 10xxxxxx}Else /** IF ((Code >> 1) & 0xFF) = = 0x7E)*/ {                //Six bytes                //1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx            }        }         returnRes.join (' '); }, encode:function(str) {if(!str) {            return‘‘; }        varUTF8 = This. Utf16toutf8 (str);//turn into UTF8        vari = 0;//Traverse Index        varLen =utf8.length; varres = [];  while(I <Len) {            varC1 = Utf8.charcodeat (i++) & 0xFF; Res.push ( This. TABLE[C1 >> 2]); //need to fill 2 x =            if(i = =Len) {Res.push ( This. table[(C1 & 0x3) << 4]); Res.push (' = = ');  Break; }            varC2 = Utf8.charcodeat (i++); //need to fill 1 x =            if(i = =Len) {Res.push ( This. table[((C1 & 0x3) << 4) | ((C2 >> 4) & 0x0F)]); Res.push ( This. table[(C2 & 0x0F) << 2]); Res.push (=);  Break; }            varC3 = 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]); }         returnRes.join (' '); }, Decode:function(str) {if(!str) {            return‘‘; }         varLen =str.length; vari = 0; varres = [];  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)); if(Code3! = 64) {Res.push (String.fromCharCode (C2)); }            if(Code4! = 64) {Res.push (String.fromCharCode (C3)); }         }         return  This. Utf8toutf16 (Res.join (")); }, Decodetobytes:function(str) {if(!str) {            return‘‘; }         varLen =str.length; vari = 0; varres = [];  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 (C1); if(Code2! = 64) {Res.push (C2); }            if(Code3! = 64) {Res.push (C3);         }} res.pop (); returnRes; }}; 
View Code

Gzip transcoding and decoding (Zlib.js library)

Https://github.com/imaya/zlib.js/blob/master/README.en.md

Comes with an Encoding conversion library (text-encoding)

Https://github.com/inexorabletash/text-encoding

Use of Base64 and gzip in JavaScript

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.