Skip to content
Tutorials Center
My learning

Topics

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.

NODE
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
buf.readUInt16BE(0); // 0x1234 = 4660
buf.readUInt16LE(0); // 0x3412 = 13330
buf.readUInt32BE(0); // 0x12345678

Try it

Node.js Byte order and sign

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.

Node.js Odd sizes and errors

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

NODE
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_RANGE when the read would go past the end of the buffer (the message shows the largest valid offset), when offset is negative or not an integer, or when byteLength is outside 1 to 6.
  • ERR_INVALID_ARG_TYPE when offset is 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 (0x1234 read as 0x3412).
  • Aliases: readUint8, readUint16LE and so on (lowercase int) are identical to the readUInt* forms.
  • No bounds shortcuts: unlike typed arrays, which return undefined for a bad index, these methods throw, which turns truncated input into a catchable error.
  • Floating point is read with readFloatBE() and readDoubleLE().
  • Signed values use two's complement: 0xf0 is 240 as readUInt8 and -16 as readInt8.
  • Use DataView when you want to choose the endianness at run time with a flag, or read from a plain ArrayBuffer.
  • Check lengths first when parsing untrusted data: compare buf.length with 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.