Sub topic 9
DataView: Reading and Writing Mixed Types
This chapter assumes you already know how to create an ArrayBuffer, how typed array views work, and what little-endian and big-endian byte order mean.
Typed arrays work well when every element has the same type and the same alignment. Binary formats such as file headers, network packets and device protocols rarely look like that. They pack a 1-byte flag next to a 2-byte ID next to a 4-byte float, often at odd offsets, in a byte order the format defines. A DataView handles this case. It is a view over a buffer that lets you read or write any numeric type at any byte offset, with the byte order you choose on each call.
Creating a DataView
The constructor takes a buffer and an optional byte offset and byte length:
const buf = new ArrayBuffer(16);
const whole = new DataView(buf); // all 16 bytes
const part = new DataView(buf, 4, 8); // bytes 4 through 11
console.log(whole.byteLength, part.byteOffset, part.byteLength); // 16 4 8A DataView has no element type, so offsets are always in bytes and there are no alignment rules. new DataView(buf, 3) works fine, while new Uint32Array(buf, 3) throws.
Getters, setters, and the endianness flag
Each numeric type has a getter and a setter: getInt8/setInt8, getUint16, getInt32, getFloat32, getFloat64, getBigInt64, and so on. Every multi-byte method takes a final littleEndian argument. If you leave it out, the default is big-endian, whatever byte order the platform uses.
const dv = new DataView(new ArrayBuffer(6));
dv.setUint16(0, 0x1234); // big-endian by default
dv.setUint32(2, 0xdeadbeef, true); // little-endian
console.log(dv.getUint8(0).toString(16)); // "12"
console.log(dv.getUint8(2).toString(16)); // "ef"
console.log(dv.getUint32(2, true).toString(16)); // "deadbeef"Setters convert values the same way typed arrays do. setUint8(0, 300) stores 44 because the value wraps modulo 256, and setFloat32 rounds to single precision.
Mixed fields at unaligned offsets
DataView exists for layouts like this one: a 1-byte version, then fields that start at offsets 1 and 3.
const buf = new ArrayBuffer(7);
const dv = new DataView(buf);
dv.setUint8(0, 1); // version
dv.setUint16(1, 513); // id, big-endian, offset 1 (unaligned)
dv.setFloat32(3, 2.5); // value, big-endian, offset 3 (unaligned)
console.log(dv.getUint8(0), dv.getUint16(1), dv.getFloat32(3)); // 1 513 2.5A Uint16Array or Float32Array cannot be placed at offsets 1 or 3. A later chapter turns this pattern into a full record parser.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Omitting `littleEndian` when reading little-endian data | Values come out byte-swapped, for example `0x3412` instead of `0x1234` | Pass `true` on every multi-byte call, or store it in a constant |
| Reading past the end, e.g. `getUint32(4)` on a 6-byte view | `RangeError` | Check `offset + size <= dv.byteLength` |
| Passing a typed array to the constructor | `TypeError` | Use `new DataView(u8.buffer, u8.byteOffset, u8.byteLength)` |
| Mixing up the two offsets | Reads hit the wrong bytes | Getter offsets are relative to the view's `byteOffset`, not to the start of the buffer |
Exercise
After const dv = new DataView(new ArrayBuffer(2)); dv.setUint16(0, 0x1234);, what does dv.getUint16(0, true) return, and why?
Show answer
It returns 0x3412 (13330). The setter used the big-endian default, so the bytes are 12 34. Reading them as little-endian treats byte 0 as the low byte.
Exercise
You have a Uint8Array named packet that is a subarray starting at byte 10 of a larger buffer. How do you create a DataView so that getUint16(0) reads the first two bytes of packet?
Show answer
new DataView(packet.buffer, packet.byteOffset, packet.byteLength). Using new DataView(packet.buffer) alone would start at byte 0 of the underlying buffer instead of byte 10.