Convert base64 string to ArrayBuffer Convert base64 string to ArrayBuffer arrays arrays

Convert base64 string to ArrayBuffer


Try this:

function _base64ToArrayBuffer(base64) {    var binary_string = window.atob(base64);    var len = binary_string.length;    var bytes = new Uint8Array(len);    for (var i = 0; i < len; i++) {        bytes[i] = binary_string.charCodeAt(i);    }    return bytes.buffer;}


Using TypedArray.from:

Uint8Array.from(atob(base64_string), c => c.charCodeAt(0))

Performance to be compared with the for loop version of Goran.it answer.


Goran.it's answer does not work because of unicode problem in javascript - https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding.

I ended up using the function given on Daniel Guerrero's blog: http://blog.danguer.com/2011/10/24/base64-binary-decoding-in-javascript/

Function is listed on github link: https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js

Use these lines

var uintArray = Base64Binary.decode(base64_string);  var byteArray = Base64Binary.decodeArrayBuffer(base64_string);