-
-
Save bytespider/988463 to your computer and use it in GitHub Desktop.
Base64 Encode
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 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 | |
} |
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,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} |
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
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. |
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": "Base64 Encode", | |
"keywords": [ | |
"140bytes", | |
"base64", | |
"encode" | |
] | |
} |
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
I really don't know much about encoding strings and stuff, so you tell me if I'm doing something wrong, but...
Comparing to Base64 module for Ruby - http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html - it gives something very different.
Here's my test:
And in Ruby 1.9.3:
Note that I prepended the suggest character map into the function.