• Node.js Video Tutorials

Node.js - Buffer.readFloatBE() Method



The Node.JS Buffer.readFloatBE() method helps to read a big-endian float 32-bit number at a given offset from the current buffer object.

A float 32-bit is also called FP32 or float32. When used it takes up 32-bit of memory in your computer.

The float 32-bit is divided as sign, exponent, and significand precision. The sign takes 1 bit, exponent takes 8 bits and the rest 24 (23 used for explicit storage) bits are taken by significand precision.

  • The sign bit tells about the sign of the number positive or negative.

  • The exponent is an 8 bits unsigned integer and has minimum value as 0 and maximum value as 255.

  • The significand is 24 bits. For example, a number is 125.3. So here the integer value 1253 is the significand, $10^{-1}$ is power term, with -1 as the exponent.

Syntax

Following is the syntax of the Node.JS Buffer.readFloatBE() Method

buf.readFloatBE([offset])

Parameters

  • offset − The offset that indicates the position to start reading. The offset is greater than or equal to 0 and also less than or equal to buffer.length-4. The default value is 0.

Return value

This method returns the 32-bit big endian float number from the current buffer at the given offset.

Example

To create a buffer, we are going to make use of NodeJS Buffer.from() method −

const buffer = Buffer.from([11, 12, 13, 14, 15, 16, 17, 18]);
console.log(buffer.readFloatBE(0));
console.log(buffer);

Output

The offset we have used with this method is 0. The 32-bit float big endian number at the 0th position will be returned. The buffer length created for the above is 8. So we can only use the offset with the value 0 to 4. If any value >0 it will give an error ERR_OUT_OF_RANGE.

2.697284047672242e-32
<Buffer 0b 0c 0d 0e 0f 10 11 12>

Example

Let us create a buffer with 16 bits and see the value returned using the Node.JS Buffer.readFloatBE() method.

const buffer = Buffer.from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
console.log("Length of buffer is ", buffer.length);  
console.log("Reading at big integer at offset 1:", buffer.readFloatBE(1));

Output

In the above example, the length of the buffer created is 16. So for offset, we can use the value from 0 to 8.

Length of buffer is 16
Reading at big integer at offset 1: 9.625513546253311e-38

Example

This example will check on the error if the offset is greater than the buffer.length −4. Let us create a buffer having a length of 8 and with that, you can offset the value as 0 to 4.

const buffer = Buffer.from([1, 2 ,3, 4, 5, 6, 7, 8]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 5:", buffer.readFloatBE(5));

Output

Since we have used an offset greater than 4 in the above program, it will throw an error as shown below −

buffer length is 8
internal/buffer.js:58
   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
   at boundsError (internal/buffer.js:58:9)
   at Buffer.readFloatBackwards [as readFloatBE] (internal/buffer.js:414:5)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:3:59)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
   at internal/main/run_main_module.js:21:11
nodejs_buffer_module.htm
Advertisements