According to MSDN I can create a Uint32Array in 3 ways:
new Uint32Array( length );new Uint32Array( array );new Uint32Array( buffer, byteOffset, length );
First and second method work great, but third didn’t work for me. What’s wrong in this code?
var buffer = new ArrayBuffer(8);
var uint32s = new Uint32Array(buffer, 4, 4);
uint32s[0] = 0x05050505;
var uint8s = new Uint8Array(buffer);
for (var i =0; i< 8; i++)
{
alert(uint8s[i]);
}
This works fine, but of course byteOffset = 0.
var uint32s = new Uint32Array(buffer);
It seems that the documentation is incorrect here in that
lengthis not a number of bytes but the number of 32-bit integers that theUint32Arraywill contain.Exhibit A
Changing the code to
var uint32s = new Uint32Array(buffer, 4, 1);works.Exhibit B
The documentation for Uint32Array on MDN says that
lengthis a count of items, not bytes.Exhibit C
It doesn’t really make sense to have the constructor accept a length in bytes. What should happen if
lengthis not a multiple of 4?