-
-
Save borismus/1032746 to your computer and use it in GitHub Desktop.
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; | |
} |
One liner to convert Base64 to Uint8Array:
function Uint8ArrayFromBase64(base64)
{
return Uint8Array.from(window.atob(base64), (v) => v.charCodeAt(0));
}
One liner to convert Base64Url to Uint8Array:
function Uint8ArrayFromBase64Url(base64Url)
{
return Uint8Array.from(window.atob(base64Url.replace(/-/g, "+").replace(/_/g, "/")), (v) => v.charCodeAt(0));
}
ERROR: Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded
I am running it with typescript react. Any idea?
Thanks for this! The only solution that worked for me @borismus
How to convert this recordfile:///var/mobile/Containers/Data/Application/9C0F0870-EA7F-4CD2-9C42-30A3E9862012/Library/Caches/hello.m4a into binary format.
Thank you so much sir, very respects <3
Thank you very much for this, after a week of pulling my hair out. This is the only solution that works for me.
Thank you very much, you save my work
Thank you, good work !
Thank you man! you save my day!
Thanks buddy, save my day!
It can be even shorter (1 line): https://gist.github.com/av01d/e60f6edf3c5be6cd403eabb3d1f838f4
Thank you, @borismus!