• Node.js Video Tutorials

Node.js - Buffer.readUInt32BE() Method



The NodeJS Buffer.readUInt32BE() method helps in reading an unsigned 32-bit integer at a given offset from the buffer in big endian form. The range of values that can be stored with unsigned 32-bit integer is 0 to 4294967295.

Syntax

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

buf.readUInt32BE(offset)

Parameters

This method accepts a single parameter. The same is explained below.

  • 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 unsigned integer value of the contents of 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([0,15,10,12,13,14,22,25]);
console.log("buffer stored in memory as", buffer);  
console.log("Reading 16 bit unsigned integer at offset 0:", buffer.readUInt32BE(0));

Output

The offset we have used with this method is 0. The 32-bit signed integer 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 values 0,1 ,2 and 3. If any value >3 it will give an error ERR_OUT_OF_RANGE.

buffer stored in memory as <Buffer 00 0f 0a 0c 0d 0e 16 19>
Reading 16 bit unsigned integer at offset 0: 985612

Example

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

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

Output

Length of buffer is 16
Reading at big integer at offset 2: 33752069

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 values from 0 to 4. In the example below we are making use of offset 7, so it will throw an error as shown in the output.

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 7:", buffer.readUInt32BE(7));

Output

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 7
   at boundsError (internal/buffer.js:58:9)
   at Buffer.readUInt32BE (internal/buffer.js:378: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