1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JavaScript处理base64编码的代码示例

JavaScript处理base64编码的代码示例

时间:2022-07-24 17:48:36

相关推荐

JavaScript处理base64编码的代码示例

web前端|js教程

前端,javascript

web前端-js教程

本篇文章给大家带来的内容是关于JavaScript处理base64编码的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

shop java 源码下载,vscode转rem插件,ubuntu连接vnc,开机tomcat自启,福清爬虫,php提交post请求,晋城seo优化有哪些,jquery 网站 源码下载,织梦模板仿家庭在线网lzw

因为项目需求,需要处理base64编码,再次记录,便于之后调用

进销存源码,vscode关联,ubuntu 小米3,tomcat降级,sqlite结果排序,阿里云服务器怎么搭建,帝国cms 投票插件,iris与前端框架,起点 爬虫,php新建文件夹,店铺seo优化,房地产网站,易语言 网页游戏脱机辅助教程,index.htm模板,html 404 页面 源码,semcms外贸网站管理系统,波形处理程序集lzw

关于base64:

手机如何播放在线视频网站源码,ubuntu iso装系统,osx爬虫开源项目,php nocache,seo受益问题lzw

base64的本质就是把每8位的ASCII编码变成另外一个每6位的编码,用另外一个参照表进行对应翻译。

以下为base64的js:

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, , a, , c, d, e, f, g, h, i, j, k, l, m, , o, p, q, , s, , u, v, w, x, y, z, , 1, 2, 3, 4, 5, 6, 7, 8, 9, +, / ], UTF16ToUTF8 : function(str) { var res = [], len = str.length; for (var i = 0; i 0x0000 && code = 0x0080 && code > 6) & 0x1F);// 10xxxxxxvar byte2 = 0x80 | (code & 0x3F);res.push( String.fromCharCode(byte1), String.fromCharCode(byte2)); } else if (code >= 0x0800 && code > 12) & 0x0F);// 10xxxxxxvar byte2 = 0x80 | ((code >> 6) & 0x3F);// 10xxxxxxvar byte3 = 0x80 | (code & 0x3F);res.push( String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode(byte3)); } else if (code >= 0x00010000 && code = 0x00200000 && code = 0x04000000 && code <= 0x7FFFFFFF)*/ {// 六字节// U+04000000 – U+7FFFFFFF1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(\); }, UTF8ToUTF16 : function(str) { var res = [], len = str.length; var i = 0; for (var i = 0; i > 7) & 0xFF) == 0x0) {// 单字节// 0xxxxxxxres.push(str.charAt(i)); } else if (((code >> 5) & 0xFF) == 0x6) {// 双字节// 110xxxxx 10xxxxxxvar code2 = str.charCodeAt(++i);var byte1 = (code & 0x1F) <> 4) & 0xFF) == 0xE) {// 三字节// 1110xxxx 10xxxxxx 10xxxxxxvar code2 = str.charCodeAt(++i);var code3 = str.charCodeAt(++i);var byte1 = (code <> 2) & 0x0F);var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);var utf16 = ((byte1 & 0x00FF) <> 3) & 0xFF) == 0x1E) {// 四字节// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (((code >> 2) & 0xFF) == 0x3E) {// 五字节// 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {// 六字节// 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(\); }, encode : function(str) { if (!str) { return \; } var utf8 = this.UTF16ToUTF8(str); // 转成UTF8 var i = 0; // 遍历索引 var len = utf8.length; var res = []; while (i > 2]); // 需要补2个= if (i == len) {res.push(this.table[(c1 & 0x3) << 4]);res.push(==);break; } var c2 = utf8.charCodeAt(i++); // 需要补1个= if (i == len) {res.push(this.table[((c1 & 0x3) <> 4) & 0x0F)]);res.push(this.table[(c2 & 0x0F) << 2]);res.push(=);break; } var c3 = utf8.charCodeAt(i++); res.push(this.table[((c1 & 0x3) <> 4) & 0x0F)]); res.push(this.table[((c2 & 0x0F) <> 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 <> 4); res.push(String.fromCharCode(c1)); if (code3 != -1) {c2 = ((code2 & 0xF) <> 2);res.push(String.fromCharCode(c2)); } if (code4 != -1) {c3 = ((code3 & 0x3) << 6) | code4;res.push(String.fromCharCode(c3)); } } return this.UTF8ToUTF16(res.join(\)); }};

使用:

先引入JS:

直接使用其中的方法:

Base64.decode(content);

关于图片的base64:

在图片中直接将src设置为你的base64就可以实现解码显示了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。