Search code examples
javascriptintegerarraybuffer

Convert JavaScript ArrayBuffer to array of 8-bit numbers


I just converted an array of 8-bit numbers into an ArrayBuffer. Now I would like to convert it back to an array of 8-bit (1 byte) integers to double check they match. Wondering how to do that.


Solution

  • Try

    let d = [10,20,40,50]
    let u8b = new Uint8Array(d).buffer; // array buffer
    let u8 = new Uint8Array(u8b);
    let a = Array.from(u8);
    
    console.log('d',d);
    console.log('u8b',u8b);
    console.log('u8',u8);
    console.log('a',a);