Open In App

Node.js Buffer.writeInt32LE() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Buffer.writeInt32LE() method is used to write specified bytes into the buffer using little-endian format. The value contains a valid signed 32-bit integer. If the value contains other then signed 32-bit integer then its behavior is undefined.

Syntax:

Buffer.writeInt32LE( value, offset )

Parameters: This method accept two parameters as mentioned above and described below:

  • value: It is an integer value that represents the value written into the buffer.
  • offset: It is an integer value and it represents the number of bytes to skip before starting to write and the value of offset lies within the range 0 to buffer.length – 4. It’s default value is 0.

Return value: It returns the offset plus number of bytes written.

Example 1:




// Node.js program to demonstrate the  
// Buffer.writeInt32LE() Method
  
// Allocate a buffer
const buf = Buffer.allocUnsafe(8);
  
// Write the buffer element in LE format
buf.writeInt32LE(0x05060708, 0);
  
// Display the buffer list
console.log(buf);
  
// Write the buffer element in LE format
buf.writeInt32LE(0x05060708, 4);
  
// Display the buffer list
console.log(buf);


Output:

<Buffer 08 07 06 05 00 00 00 00>
<Buffer 08 07 06 05 08 07 06 05>

Example 2:




// Node.js program to demonstrate the  
// Buffer.writeInt32LE() Method
  
// Allocate a buffer
const buf = Buffer.allocUnsafe(8);
  
// Write the buffer element in LE format
buf.writeInt32LE(0x12345678, 0);
  
// Display the buffer list
console.log(buf);
  
// Write the buffer element in LE format
buf.writeInt32LE(0x123456, 4);
  
// Display the buffer list
console.log(buf);


Output:

<Buffer 78 56 34 12 63 65 73 73>
<Buffer 78 56 34 12 56 34 12 00>

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/buffer.html#buffer_buf_writeint32le_value_offset



Last Updated : 13 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads