• Node.js Video Tutorials

Node.js - Buffer.writeFloatBE() Method



The Node.JS Buffer.writeFloatBE() method helps to write a big-endian float 32-bit number at a given offset to 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.writeFloatBE() Method

buf.writeFloatBE(value[, offset])

Parameters

This method accepts two parameters. The same is explained below.

  • value − (required) A value that has to be written to the buffer.

  • offset − (optional) The offset here indicates the position to start writing. 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

The method Buffer.writeFloatBE() writes the given value and returns offset plus the number of bytes written.

Example

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

const buffer = Buffer.alloc(10);
buffer.writeFloatBE(0xfeaabe, 0);
console.log(buffer);

Output

The offset we have used is 0. On execution the value starting from 0th position will be written to the buffer created. The buffer length created for the above is 10. So we can only use the offset with values from 0 to 6. If any value >6 it will give an error ERR_OUT_OF_RANGE.

<Buffer 4b 7e aa be 00 00 00 00 00 00>

Example

In this example will use an offset that is greater than buffer.length − 4. It should throw an error as shown below.

const buffer = Buffer.alloc(10);
buffer.writeFloatBE(0xfeaabe, 7);
console.log(buffer);

Output

internal/buffer.js:83
   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 <= 6. Received 7
   at boundsError (internal/buffer.js:83:9)
   at checkBounds (internal/buffer.js:52:5)
   at Buffer.writeFloatBackwards [as writeFloatBE] (internal/buffer.js:941:3)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8)
   at Module._compile (internal/modules/cjs/loader.js:1063:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
   at Module.load (internal/modules/cjs/loader.js:928:32)
   at Function.Module._load (internal/modules/cjs/loader.js:769:14)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
   at internal/main/run_main_module.js:17:47 {
      code: 'ERR_OUT_OF_RANGE'
   }

Example

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

const buffer = Buffer.allocUnsafe(10);
buffer.writeFloatBE(0xfeaabe);
console.log(buffer);

Output

The offset is not used, by default it will be taken as 0. So the output for above is as shown below.

<Buffer 4b 7e aa be 01 00 00 00 00 00>
nodejs_buffer_module.htm
Advertisements