Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js buf.writeUInt32LE() and other integer writers

Reference for the Node.js Buffer integer writers: writeUInt8, writeInt16BE, writeUInt32LE, writeBigInt64, writeUIntBE and more, with range checks and return values.

The write* methods of Buffer encode a number into a fixed number of bytes at a given offset. The naming matches the readers: signed or unsigned (UInt or Int), the bit size, and the byte order BE (big-endian) or LE (little-endian). writeUInt16BE(0x1234, 0) stores the bytes 12 34.

Each method returns the offset just after the bytes it wrote, so you can chain calls to build a record without tracking positions by hand. The value is range-checked: a number that does not fit in the field throws instead of being silently cut.

NODE
const buf = Buffer.alloc(6);
let at = 0;
at = buf.writeUInt16BE(0xcafe, at);   // 2
at = buf.writeUInt32LE(1, at);        // 6

Try it

Node.js Building a record

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 Range checks and odd sizes

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.writeUInt8(value, offset = 0)           // returns number
buf.writeInt8(value, offset = 0)
buf.writeUInt16LE(value, offset = 0)        // also writeUInt16BE
buf.writeInt16LE(value, offset = 0)         // also writeInt16BE
buf.writeUInt32LE(value, offset = 0)        // also writeUInt32BE
buf.writeInt32LE(value, offset = 0)         // also writeInt32BE
buf.writeBigUInt64LE(value, offset = 0)     // value is a bigint; also writeBigUInt64BE
buf.writeBigInt64LE(value, offset = 0)      // also writeBigInt64BE
buf.writeUIntLE(value, offset, byteLength)  // 1 to 6 bytes; also writeUIntBE
buf.writeIntLE(value, offset, byteLength)   // 1 to 6 bytes; also writeIntBE
Name Description
value The number (or bigint for the 64-bit forms) to store. Must fit the field.
offset Index of the first byte to write. An integer from 0 to buf.length - size.
byteLength For the variable-length forms, 1 to 6.

Returns: offset + number of bytes written.

Errors and edge cases:

  • ERR_OUT_OF_RANGE when value is outside the range of the type (the message states the allowed range), when the write would pass the end of the buffer, or when offset is negative or fractional.
  • The 64-bit methods require a bigint; a plain number throws a TypeError.
  • A fractional value is truncated toward zero and NaN is written as 0, without an error.

Usage notes

  • Check ranges at the boundary you control. The thrown error is helpful for a bug, but for user input validate first to give a proper message.
  • The returned offset makes a sequence of writes read like a format description. Compute the total size first and allocate it with Buffer.alloc().
  • Numbers above 2^53 lose precision before they get here; use bigint with the 64-bit writers.
  • Same byte-order rules as reading: match the specification of the format. See integer readers.
  • Aliases: writeUint8, writeUint32LE and the other lowercase int spellings are identical.
  • Writing floats is a separate method family: writeFloatBE() and writeDoubleLE().
  • Writing text uses buf.write(string, offset, length, encoding), which returns the number of bytes written and stops at a character boundary when space runs out.
  • Large numbers of records: reuse one buffer and fill() or overwrite it instead of allocating each time.