Node.js Buffer.readFloatBE() Method

The Buffer.readFloatBE() Method is an inbuilt application programming interface of class Buffer within the Buffer module which is used to read big endian 32 bits floating-point value to an allocated buffer at the specified offset.

Syntax:

Buffer.readFloatBE( offset )

Parameters: This method accepts single parameter offset which specifies the number of bytes to skip before read. The value of offset lies 0 <= offset <= Buffer.length – 4. It’s default value is 0.

Return Value: It returns an integer value in big endian format.

Below examples illustrate the use of buf.readFloatBE() Method in Node.js:

Example 1:




// Node program to demonstrate the  
// Buffer.readFloatBE(INTEGER) method 
// Creating a buffer of given size 
const buf = Buffer.from([10, 20, 30, 40, 50, 60, 70, 80]);
  
// Display the result 
console.log("Functions of Buffer.readFloatBE(int)");
console.log(buf.readFloatBE(0))
console.log(buf); 


Output:

Functions of Buffer.readFloatBE(int)
7.13161255447549e-33
<Buffer 0a 14 1e 28 32 3c 46 50>

Example 2:




// Node program to demonstrate the  
// Buffer.readFloatBE(INTEGER) method 
// Creating a buffer of given size 
const buf = Buffer.from([100, 200, 300, 400, 500, 600, 700, 800]);
  
// Display the result 
console.log("Functions of Buffer.readFloatBE(int)"); 
console.log(buf.readFloatBE(5))
console.log(buf); 


Output:

Functions of Buffer.readFloatBE(int)
internal/buffer.js:72
  throw new ERR_OUT_OF_RANGE(type || 'offset',
  ^

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
It must be >= 0 and <= 4. Received 5
   . . .

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

Reference: https://nodejs.org/docs/latest-v11.x/api/buffer.html#buffer_buf_readfloatbe_offset


Contact Us