Skip to content

Instantly share code, notes, and snippets.

@paulcuth
Created November 29, 2011 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcuth/1407111 to your computer and use it in GitHub Desktop.
Save paulcuth/1407111 to your computer and use it in GitHub Desktop.
JavaScript implementation of base64 encoding.
function base64Encode (data) {
var encoded = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
chars, bin,
output = '',
i;
while (data) {
chars = data.substr (0, 3) + String.fromCharCode (0, 0);
bin = '';
for (i = 0; i < 3; i++) bin += ('0000000' + chars.charCodeAt (i).toString (2)).substr (-8);
for (i = 0; i < 4; i++) output += encoded.substr (parseInt (bin.substr (i * 6, 6), 2), 1);
data = data.substr (3);
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment