how do i turn an array of bits as strings into an arraybuffer?

You can use join, to join 8 bits together, and then use parseInt with 2 as the radix to convert the binary number, and then place in the arrayBuffer.

Below is an example.

//lets make some demo data.
const data = new Array(256).fill().map(m => Math.random() < 0.5 ? '1' : '0');

//convert data of '0', '1' into ArrayBuffer
const buffer = new Uint8Array(32);
let bpos = 0;
for (let l = 0; l < data.length; l += 8) {
  const b = data.slice(l, l + 8).join(''); 
  buffer[bpos] = parseInt(b, 2);
  bpos += 1;
}

console.log(buffer);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top