In JavaScript, which byte order does DataView's getUint32 use when littleEndian is omitted, and how does that differ from reading a Uint32Array?
answer
- one default, independent of the machine
- most significant byte first
- typed arrays follow the platform
- the flag belongs to each call
basics
~20 sDataView's getUint32 reads big-endian when littleEndian is omitted or false, on every platform. A Uint32Array always uses the platform's native byte order, which on most machines is little-endian, so the two usually disagree over the same bytes.
solid answer
~40 s`DataView` getters and setters default to big-endian: omitting `littleEndian`, or passing `false` or `undefined`, reads the most significant byte first on every machine. A typed array has no such option. Its multi-byte elements always use the platform's native order, and most platforms are little-endian. So over the bytes `12 34 56 78`, `view.getUint32(0)` gives `0x12345678`, `view.getUint32(0, true)` gives `0x78563412`, and `new Uint32Array(buffer)[0]` gives `0x78563412` on a little-endian host. The flag belongs to each call rather than to the view, so a parser has to pass it on every multi-byte access. It is the last parameter: `getUint32(offset, littleEndian)` and `setUint32(offset, value, littleEndian)`. Single-byte accessors such as `getUint8` take no flag, since one byte has no order. Reading with the wrong order returns a plausible number and raises no error.
code
javascript · 7 linesconst buffer = new Uint8Array([0x12, 0x34, 0x56, 0x78]).buffer;
const view = new DataView(buffer);
console.log(view.getUint32(0).toString(16)); // 12345678
console.log(view.getUint32(0, true).toString(16)); // 78563412
// platform dependent: this is the little-endian result
console.log(new Uint32Array(buffer)[0].toString(16)); // 78563412go deeper
Recall the one fact most people get backwards: DataView is big-endian unless told otherwise. Remember too that typed arrays offer no choice, since they use whatever the machine uses.
Explain why the same four bytes produce different numbers, and walk through the 12 34 56 78 example for both orders. Point out that the flag is per call and is the last parameter.
Connect the default to the bugs it causes: a missing flag on one accessor gives one wrong field among correct ones. Explain why tests on a single architecture never reveal typed array reliance on native order.
The tradeoff angle is where the decision lives in a codebase: confine byte order to one small reader per format, so that no call site can forget a flag and reviewers have one place to check.
## What byte order means A number wider than one byte has to be laid out across several bytes in memory, and there are two conventions for the order. **Big-endian** puts the most significant byte first. **Little-endian** puts the least significant byte first. For the 32-bit value `0x12345678`: | Order | Byte 0 | Byte 1 | Byte 2 | Byte 3 | |---|---|---|---|---| | Big-endian | `12` | `34` | `56` | `78` | | Little-endian | `78` | `56` | `34` | `12` | The bytes in memory carry no marker saying which convention wrote them. Whoever reads them has to know, and reading with the wrong one returns a different number with no error of any kind. ## DataView: explicit, with a big-endian default A **`DataView`** gives you typed accessors over an `ArrayBuffer` and lets you pick the order on every access: - `getUint32(byteOffset, littleEndian)` reads, and `setUint32(byteOffset, value, littleEndian)` writes. - If `littleEndian` is `false` or `undefined`, the value is read or written as **big-endian**, whatever the machine. - If it is `true`, the value is read or written as little-endian, again whatever the machine. - The accessors for one byte, `getUint8`, `getInt8` and their setters, take no such argument, because a single byte has no order. The same flag applies to every multi-byte accessor, floats and BigInt values included: `getInt16`, `getFloat32`, `getFloat64`, `getBigUint64` and their setters. There is no way to set it once on the view, so each call has to state it. The default surprises people because most machines are little-endian, which makes omitting the flag look like it should mean the common case. It means the opposite. ## Typed arrays: always the platform's order A **typed array** has no such argument. All element types wider than one byte use the host's native order, and the specification leaves that to the platform. The benefit is that reading a `Float64Array` of numbers that your own program wrote needs no conversion, so it stays direct and efficient. The cost is that the same bytes can give different numbers on machines with different byte orders, which matters as soon as the bytes came from outside the program. `Uint8Array`, `Int8Array` and `Uint8ClampedArray` hold one byte per element, so they read the same on every host, which is why a byte dump is a safe way to inspect a buffer. ```js const buffer = new Uint8Array([0x12, 0x34, 0x56, 0x78]).buffer; const view = new DataView(buffer); console.log(view.getUint32(0).toString(16)); // 12345678 console.log(view.getUint32(0, true).toString(16)); // 78563412 // platform dependent: this is the little-endian result console.log(new Uint32Array(buffer)[0].toString(16)); // 78563412 ``` ## Writing and reading must agree The flag is not stored anywhere, so a mismatch between a write and a later read goes unnoticed: ```js const d = new DataView(new ArrayBuffer(2)); d.setUint16(0, 1, true); console.log(d.getUint8(0), d.getUint8(1)); // 1 0 console.log(d.getUint16(0)); // 256 console.log(d.getUint16(0, true)); // 1 ``` The setter wrote the bytes `01 00`. The default read takes `01` as the high byte and returns 256. A small value such as a channel count or a version number becoming a suspiciously round multiple of 256 is the classic sign of this mistake. ## Other ways DataView differs from a typed array - **No alignment rule.** A `DataView` can read at any byte offset, so `getUint32(1)` is legal. A `Uint32Array` needs a `byteOffset` that is a multiple of 4. - **Reads past the end throw.** `getUint32` throws `RangeError` when the offset and size go beyond the view, where a typed array's out-of-range index quietly gives `undefined`. - **No element type of its own.** The same `DataView` can read a `Uint16` at one offset and a `Float32` at the next, and it has no `length`, only `byteLength`. - **Signedness is per call too.** Over the bytes `ff ff ff ff`, `getInt32(0)` returns -1 and `getUint32(0)` returns 4294967295. ## Choosing between them 1. If the bytes have a defined layout from outside your program, such as a file or a network format, use `DataView` and state the format's order on each call. 2. If your own program writes and reads the same numbers in one process, use a typed array and let it use the native order. 3. If you ever need the host's order, you can detect it, but a parser for a format with a defined order should not need to.
- Does a `DataView` care about alignment the way a typed array does?No. `getUint32(1)` is legal, while `new Uint32Array(buffer, 1)` throws `RangeError` because its `byteOffset` must be a multiple of 4. A `DataView` read only has to stay inside the view: going past the end throws `RangeError`, unlike a typed array's out-of-range index, which gives `undefined`.
- How do you find out the platform's byte order, and should a file parser do it?Write 256 through `DataView.setInt16(0, 256, true)`, then read the same buffer through an `Int16Array`: getting 256 back means the host is little-endian. A file parser should not need this, because the format defines its own order, and passing that order to `DataView` works on every host.
- DataView reads in the platform's byte order unless you pass a flag.
- Omitting littleEndian means little-endian, because most hardware is.
- The littleEndian flag is set once, when the DataView is constructed.
- Typed arrays are always little-endian according to the specification.
- getUint8 also needs the littleEndian flag to read correctly.