Skip to content

Instantly share code, notes, and snippets.

@devnowcommit
Forked from subhodi/arrayBufferToBase64.js
Created December 21, 2021 18:47
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 devnowcommit/5e8cabb3386aacbe3422d6e9b1cac428 to your computer and use it in GitHub Desktop.
Save devnowcommit/5e8cabb3386aacbe3422d6e9b1cac428 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