• Node.js Video Tutorials

Node.js - Buffer.readInt32LE() Method



The NodeJS Buffer.readInt32LE() method helps in reading a signed 32-bit integer at a given offset from the buffer in little endian form. The integer read from the buffer is represented as two's complement signed values.

The range of values to be stored with a 32-bit signed integer is −2147483648 to 2147483647.

Syntax

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

buf.readInt32LE(offset)

Parameters

This method accepts a single parameter. The same is explained below.

  • offset − The offset that indicates the position to start reading. The offset is greater than or equal to 0 and also less than or equal to buffer.length-4. The default value is 0.

Return value

This method returns the 32-bit signed integer value of the contents of the current buffer at the given offset.

Example

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

const buffer = Buffer.from([0,15,10,12,13,14,22,25]);
console.log("buffer stored in memory as", buffer);  
console.log("Reading 16 bit signed integer at offset 0:", buffer.readInt32LE(0));

Output

The offset we have used with this method is 0. The 32-bit signed integer at the 0th position will be returned. The buffer length created for the above is 8. So we can only use the offset with the values 0,1 ,2 and 3. If any value >3 it will give an error ERR_OUT_OF_RANGE.

buffer stored in memory as <Buffer 00 0f 0a 0c 0d 0e 16 19>
Reading 16 bit signed integer at offset 0: 201985792

Example

Let us create a buffer with 16 bits and see the value returned using the Node.JS Buffer.readInt32LE() method.

const buffer = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7,8, 9, 10, 11,12, 13, 14, 15]);
console.log("Length of buffer is ", buffer.length);  
console.log("Reading at big integer at offset 2:", buffer.readInt32LE(2));

Output

Length of buffer is  16
Reading at big integer at offset 2: 84148994

Example

This example will check on the error if the offset is greater than the buffer.length −4. Let us create a buffer having a length of 8 and with that, you can offset the values from 0 to 4. In the example below we are making us of offset 7, so it will throw an error as shown in the output.

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 7:", buffer.readInt32LE(7));

Output

buffer length is  8
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 <= 4. Received 7
   at boundsError (internal/buffer.js:58:9)
   at Buffer.readInt32LE (internal/buffer.js:378:5)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:3:59)
   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
nodejs_buffer_module.htm
Advertisements