Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
little-endian

What is Buffer.writeDoubleLE() method in Node.js?

Umme Ammara

Endianness defines the order of the sequence of multibyte data types (int, float, etc.) stored in the computer memory.

  • In little-endian, the last byte of the binary representation of the multibyte data type is stored first.

  • In big-endian, the first byte of the binary representation of the multibyte data type is stored first.

The Buffer.writeDoubleLE() method writes a little-endian double value to a buffer at a specified offset.

Syntax

The Buffer.writeDoubleLE() method is defined as follows:

Parameters

The method takes up to two parameters:

  • value: This is the 4-byte floating-point number to be written to the buffer. If the value is not a valid 64 bits little-endian number, the behavior is undefined.

  • offset: This is the number of bytes to skip before starting the write. The default value of offset is 0.

offset is an optional parameter.

The offset must satisfy the condition: 0 <= offset <= Buffer.lengthreturns the number of bytes in the buffer - 8.

Return value

The Buffer.writeDoubleLE() method returns the sum of the offset and the number of bytes written.

Example

The code below shows the use of the Buffer.writeDoubleLE() method. We allocate an 8-byte buffer and write the value 123.456 at offset 0. We then display the buffer.

//Allocate 8 bytes buffer
const buf = Buffer.allocUnsafe(8);

//Write 123.456 to buffer at offset 0
console.log(buf.writeDoubleLE(123.456,0));

//Display buffer
console.log(buf);

RELATED TAGS

node.js
little-endian

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring