本篇文章给大家带来的内容是关于javascript处理base64编码的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
因为项目需求,需要处理base64编码,再次记录,便于之后调用
关于base64:
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', '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) { // 单字节,这里并不考虑0x0000,因为它是空字节 // u+00000000 – u+0000007f 0xxxxxxx res.push(str.charat(i)); } else if (code >= 0x0080 && code <= 0x07ff) { // 双字节 // u+00000080 – u+000007ff 110xxxxx 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) { // 三字节 // u+00000800 – u+0000ffff 1110xxxx 10xxxxxx 10xxxxxx // 1110xxxx var byte1 = 0xe0 | ((code >> 12) & 0x0f); // 10xxxxxx var byte2 = 0x80 | ((code >> 6) & 0x3f); // 10xxxxxx var byte3 = 0x80 | (code & 0x3f); res.push( string.fromcharcode(byte1), string.fromcharcode(byte2), string.fromcharcode(byte3) ); } else if (code >= 0x00010000 && code <= 0x001fffff) { // 四字节 // u+00010000 – u+001fffff 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (code >= 0x00200000 && code <= 0x03ffffff) { // 五字节 // u+00200000 – u+03ffffff 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (code >= 0x04000000 && code <= 0x7fffffff)*/ { // 六字节 // u+04000000 – u+7fffffff 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } 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) { // 单字节 // 0xxxxxxx res.push(str.charat(i)); } else if (((code >> 5) & 0xff) == 0x6) { // 双字节 // 110xxxxx 10xxxxxx 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) { // 三字节 // 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) { // 四字节 // 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 < len) { var c1 = utf8.charcodeat(i++) & 0xff; res.push(this.table[c1 >> 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) | ((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('')); }};
使用:先引入js:
<script src="include/js/base64.js" type="text/javascript"></script>
直接使用其中的方法:
base64.decode(content);
关于图片的base64:在图片中直接将src设置为你的base64就可以实现解码显示了。
以上就是javascript处理base64编码的代码示例的详细内容。