Skip to content

Instantly share code, notes, and snippets.

@anbinh
Forked from aemkei/LICENSE.txt
Last active December 15, 2015 14:29
Show Gist options
  • Save anbinh/5275169 to your computer and use it in GitHub Desktop.
Save anbinh/5275169 to your computer and use it in GitHub Desktop.
140byt.es - Base64 Encoder
# 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!
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
}
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}
{
"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