public
Last active

Convert a base64 string into a binary Uint8 Array

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var BASE64_MARKER = ';base64,';
 
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
 
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}

Thanks, you made my day.

Thanks for this script!

Small nit: your "i" needs a "var" declaration to avoid it becoming global...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.