Reputation: 3213
Should I care about big-endian machines while using Uint32Array
and other TypedArray
s that represent more than 1 byte array?
Are there devices with big-endian architecture that can correctly open modern sites that use ArrayBuffer API? How much?
All Android devices are little-endian. I assume that AMD and Intel based computers too.
For example, for little-endian:
const buffer = new TextEncoder().encode("abcd").buffer; // It's [97, 98, 99, 100] bytes
new Uint32Array(buffer); // [1684234849]
new Uint32Array(new Uint8Array([0, 0, 0, 1]).buffer); // [16777216]
new Uint32Array(new Uint8Array([1, 0, 0, 0]).buffer); // [1]
But I assume that on the big-endian machine I will get the other results: [1633837924]
, [1]
and [16777216]
correspondingly.
Also I can note that bitwise operations works transparancy with TypeArray
s, as if I just work with Number
(big-endian).
new Uint32Array(new Uint8Array([0, 0, 0, 1]).buffer)[0] >> 16; // 256
16777216 >> 16; // 256
Upvotes: 2
Views: 330
Reputation: 27539
If you're concerned about endianness, you can use the DataView
interface. This allows you to assert the endianness of a byte array, rather than relying on the platform byte order.
This may be a concern if you expect your code to run on a PowerPC platform like an XBox or PS3.
Upvotes: 3