介绍
这篇文章给大家分享的是有关JavaScript如何处理base64编码的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
因为项目需求,需要处理base64编码,再次记录,便于之后调用
<强>关于base64: 强>
base64的本质就是把每8位的ASCII编码变成另外一个每6位的编码,用另外一个参照表进行对应翻译。
以下为base64的js:
var base64={//转码表 表:( & # 39;一个# 39;& # 39;b # 39; & # 39; c # 39; & # 39; d # 39; & # 39; e # 39;, & # 39; f # 39; & # 39;舌鳎# 39;,& # 39;h # 39; & # 39;我# 39;& # 39;强生# 39;& # 39;k # 39;, & # 39; l # 39; & # 39;猴# 39;& # 39;n # 39;, & # 39; o # 39;& # 39;p # 39; & # 39;提问# 39;& # 39;" # 39;& # 39;& # 39;,& # 39;t # 39; & # 39; u # 39;, & # 39; v # 39; & # 39; w # 39;, & # 39; x # 39; & # 39;y # 39; & # 39; z # 39; & # 39;一个# 39;,& # 39;b # 39; & # 39; c # 39;, & # 39; d # 39; & # 39; e # 39;, & # 39; f # 39; & # 39;舌鳎# 39;& # 39;h # 39; & # 39;我# 39;,& # 39;强生# 39;& # 39;k # 39;, & # 39; l # 39; & # 39;猴# 39;,& # 39;n # 39; & # 39;o # 39; & # 39; p # 39; & # 39;提问# 39;& # 39;" # 39;,& # 39;& # 39;& # 39;t # 39;, & # 39; u # 39; & # 39; v # 39; & # 39;w # 39; & # 39; x # 39; & # 39; y # 39;, & # 39; z # 39; & # 39; 0 & # 39; & # 39; 1 & # 39; & # 39; 2 & # 39; & # 39; 3 & # 39; & # 39;4 & # 39;& # 39;5 & # 39;& # 39;6 & # 39;& # 39;7 & # 39;& # 39;8 & # 39;& # 39;9 & # 39;& # 39;+ & # 39;& # 39;/& # 39; ), UTF16ToUTF8:函数(str) { var res=[], len=str.length; (var=0;我& lt;兰;我+ +){ var代码=str.charCodeAt(我); 如果代码比;0 x0000,,代码& lt;=0 x007f) {//单字节,这里并不考虑0 x0000,因为它是空字节//U + 00000000 - U + 00000000 f 0 xxxxxxx res.push (str.charAt (i)); }else if(代码在=0 x0080,,代码& lt;=0 x07ff) {//双字节//U + 00000080 - U + 00000080 ff 110 xxxxx 10 xxxxxx//110 xxxxx var byte1=0 xc0 |(代码在祝辞6),0 x1f);//10 xxxxxx var byte2=0 x80 |(代码,0 x3f); res.push ( String.fromCharCode (byte1), String.fromCharCode (byte2) ); }else if(代码在=0 x0800,,代码& lt;=0 xffff) {//三字节//U + 00000800 - U + 0000 ffff 1110 xxxx 10 xxxxxx xxxxxx//1110 xxxx var byte1=0 xe0 |(代码在祝辞12),0 x0f);//10 xxxxxx var byte2=0 x80 |(代码在祝辞6),0 x3f);//10 xxxxxx var byte3=0 x80 |(代码,0 x3f); res.push ( String.fromCharCode (byte1), String.fromCharCode (byte2), String.fromCharCode (byte3) ); }else if(代码在=0 x00010000,,代码& lt;=0 x001fffff) {//四字节//U + 00010000 - U + 001 fffff 00010000 xxx 10 xxxxxx xxxxxx 10 xxxxxx }else if(代码在=0 x00200000,,代码& lt;=0 x03ffffff) {//五字节//U + 00200000 - U + 03 ffffff 00200000 xx 10 xxxxxx 10 xxxxxx xxxxxx xxxxxx }/* *如果其他(代码在=0 x04000000,,代码& lt;=0 x7fffffff) */{//六字节//U + 04000000 - U + 7 fffffff 04000000 x 10 xxxxxx xxxxxx 10 xxxxxx xxxxxx 10 xxxxxx } } 返回res.join (& # 39; & # 39;); }, UTF8ToUTF16:函数(str) { var res=[], len=str.length; var=0; (var=0;我& lt;兰;我+ +){ var代码=str.charCodeAt(我);//对第一个字节进行判断 如果(((代码在祝辞7),0 xff)==0 x0) {//单字节//0 xxxxxxx res.push (str.charAt (i)); }else if(((代码在祝辞5),0 xff)==0 x6) {//双字节//110 xxxxx 10 xxxxxx var code2=str.charCodeAt (+ + i); var byte1=(代码,0 x1f) & lt; & lt;6; var byte2=code2和;0 x3f; var utf16=byte1 | byte2; res.push (Sting.fromCharCode (utf16)); }else if(((代码在祝辞4),0 xff)==0 xe) {//三字节//1110 xxxx 10 xxxxxx xxxxxx var code2=str.charCodeAt (+ + i); var code3=str.charCodeAt (+ + i); var byte1=(代码& lt; & lt;4)| (code2祝辞祝辞2),0 x0f); var byte2=((code2,0 x03) & lt; & lt;6)| (code3,0 x3f); var utf16=((byte1,0 x00ff) & lt; & lt;8)| byte2 res.push (String.fromCharCode (utf16)); }else if(((代码在祝辞3),0 xff)==0 x1e) {//四字节//11110 xxx 10 xxxxxx xxxxxx 10 xxxxxx }else if(((代码在祝辞2),0 xff)==0 x3e) {//五字节//111110 xx 10 xxxxxx 10 xxxxxx xxxxxx xxxxxx 其他}/* *如果(((代码在祝辞1),0 xff)==0 x7e) */{//六字节//1111110 x 10 xxxxxx xxxxxx 10 xxxxxx xxxxxx 10 xxxxxx } } 返回res.join (& # 39; & # 39;); }, 编码:函数(str) { 如果(str) { 返回& # 39;& # 39;; } var utf8=this.UTF16ToUTF8 (str);//转成UTF8 var=0;//遍历索引 var len=utf8.length; var res=[]; 虽然(我& lt;len) { c1=utf8.charCodeAt var (+ +),0 xff; res.push(这一点。表(c1祝辞祝辞2);//需要补2个=如果我==len () { res.push(这一点。表[(c1,0 x3) & lt; & lt;4); res.push (& # 39;==& # 39;); 打破; } var c2=utf8.charCodeAt (+ +);//需要补1个=如果我==len () { res.push(这一点。表(((c1,0 x3) & lt; & lt;4)| (c2祝辞祝辞4),0 x0f)]); res.push(这一点。表[(c2,0 x0f) & lt; & lt;2); res.push (& # 39;=& # 39;); 打破; } var c3=utf8.charCodeAt (+ +); res.push(这一点。表(((c1,0 x3) & lt; & lt;4)| (c2祝辞祝辞4),0 x0f)]); res.push(这一点。表(((c2,0 x0f) & lt; & lt;2)| (c3,0 xc0)在祝辞6)]); res.push(这一点。表(c3,0 x3f]); } 返回res.join (& # 39; & # 39;); }, 解码:函数(str) { 如果(str) { 返回& # 39;& # 39;; } var len=str.length; var=0; var res=[]; 虽然(我& lt;len) { code1=this.table.indexOf (str.charAt (+ +)); code2=this.table.indexOf (str.charAt (+ +)); code3=this.table.indexOf (str.charAt (+ +)); code4=this.table.indexOf (str.charAt (+ +)); c1=(code1 & lt; & lt;2)| (code2祝辞祝辞4); res.push (String.fromCharCode (c1)); 如果(code3 !=1) { c2=((code2,null null null null null null null null null null null null nullJavaScript如何处理base64编码