Skip to content

Instantly share code, notes, and snippets.

@bytespider
Forked from 140bytes/LICENSE.txt
Created May 24, 2011 10:08
Show Gist options
  • Save bytespider/988463 to your computer and use it in GitHub Desktop.
Save bytespider/988463 to your computer and use it in GitHub Desktop.
Base64 Encode
function base64Encode(
a, // string to encode
b, // character table ie, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
c, // placeholder
d, // placeholder
e, // placeholder
f, // placeholder
g, // placeholder
h // placeholder
){
h="";
for(d=0; // initialise the output buffer
a[d]; // check if a value is returned
h+=b[e>>2]+b[e<<4&63|f>>4]+b[f<<2&63|g>>6||64]+b[g&63||64]) // split into 6bits characters and find it in our character table
for(c=4;c<7;)
arguments[c++]=a.charCodeAt(d++) // copy the character code into a variable declared as a placeholder
return h
}
function(a,b,c,d,e,f,g,h){h="";for(d=0;a[d];h+=b[e>>2]+b[e<<4&63|f>>4]+b[f<<2&63|g>>6||64]+b[g&63||64])for(c=4;c<7;)arguments[c++]=a.charCodeAt(d++);return h}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Rob Griffiths http://bytespider.eu
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": "Base64 Encode",
"keywords": [
"140bytes",
"base64",
"encode"
]
}
@bytespider
Copy link
Author

Correct it should be MTIz My implementation breaks because somewhere an undefined isnt being cast to 0. Ie this works
base64Encode("123", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", "", "", "", "", "", "");

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