setUint32() writes an unsigned 32-bit whole number (0 to 4294967295) into four bytes of a DataView. Pass the byte offset, the value and, for little-endian formats, true. One call replaces four separate byte writes, and the byte order argument decides which byte goes where.
Converts value to a unsigned 32-bit integer (0 to 4294967295) and writes it into the 4 bytes starting at byteOffset, big-endian. Returns undefined.
view.setUint32(byteOffset, value, true)
Writes the same 4 bytes in little-endian order, so the least significant byte lands at byteOffset.
Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Uint32Array, a DataView has no alignment rule: setUint32() works at any byte position, even one that is not a multiple of 4.
Library declarations
JavaScript
DataView.prototype.setUint32(byteOffset, value)
DataView.prototype.setUint32(byteOffset, value, littleEndian)
// returns undefined; writes 4 bytes starting at byteOffset
// value is converted to a unsigned 32-bit integer
// needs byteOffset + 4 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian
Parameters
Parameter
Description
byteOffset
Where to start writing, counted in bytes from the start of the view (not of the underlying buffer). It is converted to a whole number, so 1.9 means 1 and "2" means 2. All 4 bytes must fit: byteOffset + 4 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
value
The number to store. It is converted to a Number, the fraction is cut off (3.9 becomes 3, -3.9 becomes -3), and the result wraps around modulo 232 into 0 to 4294967295. NaN and Infinity are stored as 0.
littleEndian
Optional. Leave it out (or pass false) for big-endian: the most significant of the 4 bytes comes first. Pass true for little-endian: the least significant byte comes first. Any truthy value, such as 1, also means little-endian.
Return Value
Return Value
Description
undefined
setUint32() gives nothing back. Its result is the 4 bytes it changed inside the buffer; read them back with getUint32().
JavaScript Examples
Example 1 — Write Width and Height Into a BMP-Style Header
Write a Full HD image size as two 32-bit little-endian numbers, the way a BMP header stores them.
Two size fields. A bitmap header keeps width and height as 4-byte little-endian numbers, one after the other.
Write each with true.setUint32(0, 1920, true) puts the low byte, 80, first; 1080 starts with 38.
Check the bytes. The hex dump 80 07 00 00 38 04 00 00 is exactly what an image program expects for a 1920 × 1080 picture.
Example 2 — Split a 32-bit Number Into an IPv4 Address
Databases and logs often keep an IPv4 address as one number. Write it with setUint32() and read the four parts back.
JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 3232235786); // the address as one number
const parts = [];
for (let i = 0; i < 4; i++) {
parts.push(view.getUint8(i));
}
console.log(parts.join(".")); // 192.168.1.10