Created — forked from 140bytes/LICENSE.txt

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

140byt.es - Base64 encoder

View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
View LICENSE.txt

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!

View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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
}
View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
{
 
"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 commented

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 commented

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

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}
Owner

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
Something went wrong with that request. Please try again.