Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created July 27, 2010 12:28
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 mbostock/492147 to your computer and use it in GitHub Desktop.
Save mbostock/492147 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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