Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created May 24, 2011 16:06
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aemkei/989006 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 hacked the main code and further compression was done by me (@aemkei). Thanks to Rob Griffiths @bytespider and Jed Schmidt @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}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"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"
]
}
@jed
Copy link

jed commented May 24, 2011

did any of you guys try using .replace? seems like you could use one function that "toggles" from first binary replacement and then string building.

@arturh
Copy link

arturh commented May 24, 2011

I tried before merging the for loops, didn't work for me.

@maettig
Copy link

maettig commented Dec 5, 2011

Some ideas and notes:

  • You can use base-4 (toString(4) and parseInt(f,4)) instead of base-2. This does not save a byte but saves memory.
  • f can be omitted. Reuse d instead.
  • Is /.../(...) a shortcut for /.../.exec(...)? This does not work in any of my browsers (Firefox, Opera, IE8).

Here is the shortest working version I found:

function(a,b,c,d,e,f){for(f=c=e="";(d=a.charCodeAt(f))&&(c+=(256+d).toString(4).slice(-4)),d=c.substr(f++*3,3);)e+=b[parseInt(d,4)];return e}

@aemkei
Copy link
Author

aemkei commented Dec 5, 2011

See @nignag's version with correct padding: the https://gist.github.com/999166 - It is 110 bytes only!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment