Created
November 29, 2011 23:20
-
-
Save paulcuth/1407111 to your computer and use it in GitHub Desktop.
JavaScript implementation of base64 encoding.
This file contains 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 (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