• Node.js Video Tutorials

Node.js - Buffer.writeUIntLE() Method



The NodeJS Buffer.writeUIntLE() method helps in writing a bytelength of unsigned bytes of value at a given offset to the buffer in little endian form.

Syntax

Following is the syntax of the Node.JS Buffer.writeUIntLE() Method

buf.writeUIntLE(value, offset, bytelength)

Parameters

This method accepts three parameters. The same is explained below.

  • value − (required)An unsigned number to be written to the buffer.

  • offset − (required)The offset that indicates the position to start writing. The offset is greater than or equal to 0 and also less than or equal to buffer.length-bytelength. The default value is 0.

  • byteLength − (required) the number of bytes you want to write. The byteLength has to be between 0 to 6.

Return value

The NodeJS buffer.writeUIntLE() method writes the given value and returns offset plus the number of bytes written.

Example

To create a buffer, we are going to make use of Buffer.alloc() method −

const buffer = Buffer.alloc(10);
buffer.writeUIntLE(123, 0, 6);
console.log(buffer);

Output

The offset we have used is 0 and bytelength as 6. On execution the value starting from 0th position will be written to the buffer created. The buffer length created for the above is 10. So we can only use the offset with values from 0 to 4. If any value >4 it will give an error ERR_OUT_OF_RANGE.

<Buffer 7b 00 00 00 00 00 00 00 00 00>

Example

In this example will use an bytelength > 6. It should throw an error as shown below.

const buffer = Buffer.alloc(10);
buffer.writeUIntLE(123, 0, 8);
console.log(buffer);

Output

internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
PS C:\nodejsProject> node src/testbuffer.js
internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 7. Received 8
PS C:\nodejsProject> node src/testbuffer.js
internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "byteLength" is out of range. It must be >= 1 and <= 6. Received 8
   at boundsError (internal/buffer.js:58:9)
   at Buffer.writeUIntLE (internal/buffer.js:488:3)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
   at internal/main/run_main_module.js:21:11

Example

In this example will make use of offset greater than buffer.length − bytelength.

const buffer = Buffer.alloc(10);
buffer.writeUIntLE(123, 8, 3);
console.log(buffer);

Output

The offset has to be between 0 to 7. Since we have used 8, it will throw an error as shown below −

internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
PS C:\nodejsProject> node src/testbuffer.js
internal/buffer.js:58
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 7. Received 8
   at boundsError (internal/buffer.js:58:9)
   at checkBounds (internal/buffer.js:39:5)
   at checkInt (internal/buffer.js:46:3)
   at writeU_Int24LE (internal/buffer.js:544:3)
   at Buffer.writeUIntLE (internal/buffer.js:480:12)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
nodejs_buffer_module.htm
Advertisements