Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active October 7, 2015 23:37
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 lovasoa/3242606 to your computer and use it in GitHub Desktop.
Save lovasoa/3242606 to your computer and use it in GitHub Desktop.
Javascript function that converts an ArrayBuffer to a base64-encoded string
function ArrayBufferToBase64 (buff) {
var alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
enc = "",
n,p,bits;
d=new Uint8Array(buff);
var len = buff.byteLength*8;
for (var offset=0; offset<len; offset+=6){
n = (offset/8)|0;
p = offset%8;
bits = ((d[n]||0)<<p)>>2;
if(p>2){bits|=(d[n+1]||0)>>(10-p)}
enc += alph.charAt(bits&63);
}
enc += (p==4)?'=':(p==6)?'==':'';
return enc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment