Skip to content
Related Articles

Related Articles

Improve Article

Node.js | Buffer.writeUIntLE() Method

  • Last Updated : 30 Dec, 2019

The Buffer.writeUIntLE() method is used to write the specified bytes, using little endian, to a Buffer object. It supports up to 48 bits of accuracy. It’s behavior is undefined when you use the value anything other than unsigned integer.

Syntax:

Buffer.writeUIntLE( value, offset, byteLength )

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

  • value: It specifies the number which need to be written to the Buffer object.
  • offset: It specifies the number of bytes to skip before starting to write into the buffer. The value of offset lies 0 <= offset <= buf.length – byteLength.
  • byteLength: It specifies the number of bytes to write into the buffer. The value of byteLength lies 0 < byteLength <= 6.

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

Below examples illustrate the use of Buffer.writeUIntLE() Method in Node.js:



Example 1:




// Node.js program to demonstrate the  
// Buffer.writeUIntLE() method 
   
// Creating a buffer of size 4 
const buffer_1 = Buffer.allocUnsafe(4);
   
// Writes byteLength bytes of value to buf
// at the specified offset.
buffer_1.writeUIntLE(0x12127474, 0, 4);
   
// Display the result 
console.log(buffer_1);
   
// Creating a buffer of size 6
const buffer_2 = Buffer.allocUnsafe(6);
buffer_2.writeUIntLE(0x12127474abcd, 0, 6);
   
// Display the result 
console.log(buffer_2);

Output:

<Buffer 74 74 12 12>
<Buffer cd ab 74 74 12 12>

Example 2:




// Node.js program to demonstrate the  
// Buffer.writeUIntLE() method 
   
// Creating a buffer of given size 
const buffer = Buffer.allocUnsafe(8);
//Before writing anything
console.log("Before filling buffer");
console.log(buffer);
   
// to fill first 6 bytes, take offset 0
// and bytelength 6
console.log("After filling 6 bytes");
buffer.writeUIntLE(0xcc1267280012, 0, 6);
console.log(buffer);
   
// to fill next 2 bytes add 6 offet
// and bytelength 2
console.log("After filling next 2 bytes");
buffer.writeUIntLE(0x1112, 6, 2)
console.log(buffer);

Output:

Before filling buffer
<Buffer 00 00 00 00 00 00 00 00>
After filling 6 bytes
<Buffer 12 00 28 67 12 cc 00 00>
After filling next 2 bytes
<Buffer 12 00 28 67 12 cc 12 11>

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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :