-
-
Save anbinh/5275169 to your computer and use it in GitHub Desktop.
140byt.es - Base64 Encoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 140byt.es - Base64 Encoder | |
A simple base64 encoder. Simply pass your string and a reference to the function to get back the encoded string. | |
### Example | |
var | |
map ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", | |
sample = "140bytes rocks!", | |
encoded = base64Encode(sample, map); | |
### Acknowledgment | |
Artur Honzawa [@arturhonzawa](http://twitter.com/arturhonzawa) hacked the [main code](https://gist.github.com/988729) and further compression was done by me ([@aemkei](http://twitter.com/aemkei)). Thanks to Rob Griffiths [@bytespider](http://twitter.com/bytespider) and Jed Schmidt [@jedschmidt](http://twitter.com/jedschmidt) for some pretty good hints! | |
### Issues | |
* Proper "=" padding does not work | |
* IE's JScript fails with the RegExp | |
* Doesn't encode null bytes | |
Feel free to fork and improve! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var base64Encode = function( | |
a, // input string | |
b, // map string | |
c, // placeholder | |
d, // placeholder | |
e, // placeholder | |
f, // placeholder | |
g // placeholder | |
){ | |
for( | |
g = c = e = ""; | |
(d = a.charCodeAt(g)) && | |
(c += /.{8}$/( | |
1e7 + // make sure leading zeroes are there | |
d.toString(2) // convert to binary | |
)), | |
f = c.substr(g++ * 6, 6); // pick bits in groups of 6 | |
) e += b[parseInt(f, 2)]; // build result string | |
return e | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c,d,e,f,g){for(g=c=e="";(d=a.charCodeAt(g))&&(c+=/.{8}$/(1e7+d.toString(2))),f=c.substr(g++*6,6);)e+=b[parseInt(f,2)];return e} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Base64Encoder", | |
"description": "A JavaScript Base64 encoder in 140 Bytes.", | |
"contributors": [ | |
{ | |
"name" : "Artur Honzawa", | |
"url" : "https://github.com/arturh" | |
}, | |
{ | |
"name" : "Martin Kleppe", | |
"url" : "https://github.com/aemkei" | |
} | |
], | |
"keywords": [ | |
"base64", | |
"encode" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment