Convert ArrayBuffer to array and vice versa

I work with Parallel.js and Q.js to implement Multithreading in my application. Since I can only transfer serializable objects to Parallel.js, and I need to pass ArrayBuffer, I converted ArrayBuffer to Uint8Array as a JSON object with this JSON.stringify(new Uint8Array(bytes)), and then to Parallel.js I convert the JSON object to an object, then try Uint8Array and then to my buffer like this

var object = JSON.parse(data[0]);
var bytes = new Uint8Array(Object.keys(object).map(function(k) { return object[k]}));
var buffer = bytes.buffer;

this usually works because Uint8Array has a prototype called buffer, but the buffer is always undefined. The object and bytes are correct, when I look at the prototype of bufferbytes, I see this message: [Exception: TypeError: Method Uint8Array.buffer called on incompatible receiver #<Uint8Array>]And I really don't know how I should fix it.

Does anyone know of a fix?

+4
source share
1 answer

While I'm not sure why this does not work, you can convert the values ​​to Base64 and vice versa, as described here :

// encode
var str = base64EncArr(new Uint8Array(4)); // "AAAAAA=="
// decode
var arr = base64DecToArr("AAAAAA=="); // [0, 0, 0, 0]
0
source

All Articles