Node.js | Buffer.writeInt32LE() Method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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

My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : akshajjuneja9