Skip to content

Instantly share code, notes, and snippets.

@subhodi
Last active December 21, 2021 18:48
Show Gist options
  • Save subhodi/8aa7120b9e0c9523ecfcf27ecac1893b to your computer and use it in GitHub Desktop.
Save subhodi/8aa7120b9e0c9523ecfcf27ecac1893b to your computer and use it in GitHub Desktop.
arrayBufferToBase64 and vice versa
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment