Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Converting from a Uint8Array to an ArrayBuffer prepends 32 bytes?

Consider the input bytes that is a 400byte Uint8Array. The following code converts it first to an ArrayBuffer via the .buffer method and subsequently to a Float32Array :

  static bytesToFloatArray(bytes) {
      let buffer = bytes.buffer; // Get the ArrayBuffer from the Uint8Array.
      let floats = new Float32Array(buffer);
      return floats
  }

The surprise is that the conversion to the ArrayBuffer prepends 32 bytes - which is reflected in having 8 extra "0" float values at the beginning of the subsequent Float32Array :

enter image description here

Why does the buffer method add the 32 bytes - and how can that be avoided (or corrected) ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Why does the buffer method add the 32 bytes?

It didn't. The buffer had 432 bytes in the first place, even before the Uint8Array was created on it. The difference comes from the typed array using an offset and/or a length which essentially restrict the view to a slice of the buffer.

And how can that be avoided (or corrected)?

Use the same offset and adjusted length:

function bytesToFloatArray(bytes) {
   return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength/Float32Array.BYTES_PER_ELEMENT);
}
about 4 years ago · Juan Pablo Isaza Report

0

Post: @Bergi's answer is correct, I did not even look at the buffer Uint8 was created from.

I was not able to reproduce the behavior you had in chrome console, but you can always resort to using DataView to have fine grained control, something like this:

(beware of the endianness, and I did not test the code below, I might have done mistake in the byte orders)

let test8 = new Uint8Array(400);
test8.forEach((d,i,a) => a[i] = 0xff * Math.random() | 0);

function c8to32(uint8, endian = false){ //big endian by default
    var cpBuff = new ArrayBuffer(uint8.length),
        view = new DataView(cpBuff);
    for (let i = 0,v = void(0); i < uint8.length; i += 4){
        if(!endian) { //big
            v =   uint8[i] << 24 
                | uint8[i + 1] << 16 
                | uint8[i + 2] << 8 
                | uint8[i + 3];
        } else { //little
           v =    uint8[i] 
                | uint8[i + 1] << 8 
                | uint8[i + 2] << 16
                | uint8[i + 3] << 24;
        }
        view.setFloat32(
            i,
            v,
            endian
        );
    }
    return cpBuff;
}

document.getElementById("resultbig").textContent = c8to32(test8).byteLength;
document.getElementById("resultlittle").textContent = c8to32(test8, true).byteLength;
<div id="resultbig">test</div>
<div id="resultlittle">test</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!