Skip to content

Instantly share code, notes, and snippets.

@mbostock mbostock/base64.js
Created Jul 27, 2010

Embed
What would you like to do?
Base64.js
function encode(s) {
var c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
o = [];
for (var i = 0, n = s.length; i < n;) {
var c1 = s.charCodeAt(i++),
c2 = s.charCodeAt(i++),
c3 = s.charCodeAt(i++);
o.push(c.charAt(c1 >> 2));
o.push(c.charAt(((c1 & 3) << 4) | (c2 >> 4)));
o.push(c.charAt(i < n + 2 ? ((c2 & 15) << 2) | (c3 >> 6) : 64));
o.push(c.charAt(i < n + 1 ? c3 & 63 : 64));
}
return o.join("");
}
@mbostock

This comment has been minimized.

Copy link
Owner Author

commented Nov 19, 2010

Note that some browsers support the built-in functions atob and btoa for decoding and encoding, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.