Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
communitycreator

What is Buffer.readDoubleBE() in Node.js?

Irzum Jafri

Buffers are objects used in Node.js to read continuous streams of data. Buffers are of fixed length and each integer represents a byte.

Buffers are efficient when dealing with live data because they are in raw binary format, which helps to speed up processing. These buffers can be read when there are incoming streams of data and are commonly used on websites.

One of the methods used to read buffers is buffer.readDoubleBE.

The buffer.readDoubleBE method reads the binary data from the buffer in the big-endian order, which reads the most significant bit first and reads the data left-to-right. buffer.readDoubleBE returns the read data as a double, a number in 64 bits.

Syntax

buf.readDoubleBE(offset);
  • buf is the name of the buffer being read.
  • readDoubleBE specifies that the buffer is being read as a double (64 bit) in big-endian order.

The Buffer.readDoubleBE method has one parameter, which is the offset when reading the buffer.

The offset of the buffer specifies the number of bytes to skip before it starts to read. The default value of offset is 0; its value should be greater than or equal to 0 and less than or equal to buf.length-8.

Code

You can find some examples of how to use the Buffer.readDoubleBE method below.

//Buffer being declared with integers 1-10.
const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

//Buffer being read without an offset
console.log(buf.readDoubleBE());
//8.20788039913184e-304

//Buffer being read with an offset
console.log(buf.readDoubleBE(1));
//5.678932010640861e-299

//Buffer being read with maximum offset
//Since the length of the buffer is 10, the maximum offset value can be 10-8 i.e. 2.
console.log(buf.readDoubleBE(2));
//3.918231976612121e-294

//Buffer being read with offset greater than the max limit.
console.log(buf.readDoubleBE(3));
//RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 2.

RELATED TAGS

node.js
communitycreator
RELATED COURSES

View all Courses

Keep Exploring