Last active
October 7, 2015 23:37
-
-
Save lovasoa/3242606 to your computer and use it in GitHub Desktop.
Javascript function that converts an ArrayBuffer to a base64-encoded string
This file contains hidden or 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 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