Last active
December 21, 2021 18:48
-
-
Save subhodi/8aa7120b9e0c9523ecfcf27ecac1893b to your computer and use it in GitHub Desktop.
arrayBufferToBase64 and vice versa
This file contains 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(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