getUint32() reads four bytes from a DataView and returns an unsigned 32-bit whole number from 0 to 4294967295 (about 4.3 billion). File formats and protocols use this type for sizes, IDs, checksums, timestamps in seconds and “magic numbers” that identify a file type. It reads big-endian by default; pass true for little-endian data.
Reads the 4 bytes starting at byteOffset in big-endian order (most significant byte first) and returns an unsigned whole number (never negative) from 0 to 4294967295.
view.getUint32(byteOffset, true)
Reads the same 4 bytes in little-endian order (least significant byte first), the order used by most file formats from PCs, such as WAV and BMP.
Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Uint32Array, a DataView has no alignment rule: getUint32() works at any byte position, even one that is not a multiple of 4.
Library declarations
JavaScript
DataView.prototype.getUint32(byteOffset)
DataView.prototype.getUint32(byteOffset, littleEndian)
// returns a Number: 0 to 4294967295
// reads 4 bytes starting at byteOffset (counted from the start of the view)
// needs byteOffset + 4 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian
Parameters
Parameter
Description
byteOffset
Where to start reading, 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.
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
Number
A whole Number from 0 to 4294967295, built from the 4 bytes at byteOffset. It is never negative.
JavaScript Examples
Example 1 — Turn 4 Bytes Into a Date
Many file formats store time as the number of seconds since 1 January 1970 (UTC) in an unsigned 32-bit field.
JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 1700000000); // seconds since 1 January 1970
const seconds = view.getUint32(0);
console.log(seconds); // 1700000000
console.log(new Date(seconds * 1000).toISOString()); // 2023-11-14T22:13:20.000Z
Files announce their type. Many formats start with fixed bytes called a magic number: PNG files begin 89 50 4e 47, and PDF files begin with "%PDF" (25 50 44 46).
Compare four bytes at once. getUint32(0) turns the first 4 bytes into one number, so a single === comparison with 0x89504e47 does the job.
Anything else is unknown. The last array starts with different bytes, so neither check matches.
Example 3 — Read the Size Fields of a BMP Header
A BMP file stores the image width and height as 32-bit little-endian numbers. Read them from 8 header bytes.
JavaScript
const header = new Uint8Array([0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00]);
const view = new DataView(header.buffer);
const width = view.getUint32(0, true);
const height = view.getUint32(4, true);
console.log(width + " x " + height); // 640 x 480
console.log("big-endian would say " + view.getUint32(0)); // big-endian would say 2147614720