I have two functions for converting between Blob
s and arrays of bytes:
function arrayToBlob(data) {
return new Blob(data);
}
function blobToArray(data, callback) {
let reader = new FileReader();
reader.addEventListener("loadend", function() {
callback(Array.from(new Uint8Array(reader.result)));
});
reader.readAsArrayBuffer(data);
}
(blobToArray
takes a callback because it requires setting up an event listener.)
I expect these functions to be inverses of each other, but when I run
blobToArray(arrayToBlob([1,2,3]), console.log)
the result I get is not [1, 2, 3]
, but [49, 50, 51]
. Presumably something I'm doing is causing the numbers to be converted to their ASCII values, but I don't know which part is responsible.