Node.js Reference ยท Buffers and Encoding
Node.js buf.readUInt32LE() and other integer readers
Reference for the Node.js Buffer integer readers: readUInt8, readInt16BE, readUInt32LE, readBigInt64, readUIntBE and more, with byte order, offsets and errors.
The read* methods of Buffer decode a fixed number of bytes at a given offset into a number. The name tells you everything: U or no U for unsigned or signed, the bit size, and BE or LE for the byte order, big-endian (most significant byte first, the network order) or little-endian (least significant byte first, used by most CPUs). For example, readUInt16BE reads an unsigned 16-bit big-endian integer.
They are the building blocks of binary parsers: file headers, network packets and database pages. Every method takes only the offset, returns the value, and does not move any cursor, so you track positions yourself.
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]); buf.readUInt16BE(0); // 0x1234 = 4660 buf.readUInt16LE(0); // 0x3412 = 13330 buf.readUInt32BE(0); // 0x12345678
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
buf.readUInt8(offset = 0) // number, 0 to 255 buf.readInt8(offset = 0) // number, -128 to 127 buf.readUInt16LE(offset = 0) // also readUInt16BE buf.readInt16LE(offset = 0) // also readInt16BE buf.readUInt32LE(offset = 0) // also readUInt32BE buf.readInt32LE(offset = 0) // also readInt32BE buf.readBigUInt64LE(offset = 0) // bigint; also readBigUInt64BE buf.readBigInt64LE(offset = 0) // bigint; also readBigInt64BE buf.readUIntLE(offset, byteLength) // 1 to 6 bytes; also readUIntBE buf.readIntLE(offset, byteLength) // 1 to 6 bytes; also readIntBE
| Name | Description |
|---|---|
offset |
Index of the first byte to read. Must be an integer from 0 to buf.length - size. Default 0 (except for the variable-length forms, where it is required). |
byteLength |
For readUIntBE, readUIntLE, readIntBE, readIntLE: number of bytes, 1 to 6. |
Returns: a number; the 64-bit readers return a bigint, because 64-bit integers do not fit in a double.
Errors:
ERR_OUT_OF_RANGEwhen the read would go past the end of the buffer (the message shows the largest valid offset), whenoffsetis negative or not an integer, or whenbyteLengthis outside 1 to 6.ERR_INVALID_ARG_TYPEwhenoffsetis not a number, even a numeric string.
Usage notes
- Pick the byte order from the format, not from your machine. Network protocols and many file formats are big-endian; x86 and ARM memory dumps are little-endian. Mixing them up produces plausible but wrong values (
0x1234read as0x3412). - Aliases:
readUint8,readUint16LEand so on (lowercaseint) are identical to thereadUInt*forms. - No bounds shortcuts: unlike typed arrays, which return
undefinedfor a bad index, these methods throw, which turns truncated input into a catchable error. - Floating point is read with
readFloatBE()andreadDoubleLE(). - Signed values use two's complement:
0xf0is240asreadUInt8and-16asreadInt8. - Use
DataViewwhen you want to choose the endianness at run time with a flag, or read from a plainArrayBuffer. - Check lengths first when parsing untrusted data: compare
buf.lengthwith the size a field needs before reading, and return a "need more data" result for incomplete packets. - The counterpart for writing is
writeUInt32LE()and friends.