The Buffer.readUIntBE() method is an inbuilt application programming interface of class Buffer within the Buffer module which is used to read specified number of byte(s) value from an allocated buffer at a specified offset.
Syntax:
Buffer.readUIntBE( offset, byteLength )
Parameters: This method accept two parameters as mentioned above and described below:
- offset: It specifies the number of bytes to skip before read or simply signify the index in the buffer. The value of offset lies 0 <= offset <= Buffer.length – byteLength. Its default value is 0.
- byteLength: It specifies the number of bytes to be read. The value of byteLength loes 0 < byteLength <= 6.
Return: This method returns an integer value that read from buffer in Big Endian format.
Below examples illustrate the use of Buffer.readUIntBE() method in Node.js:
Example 1:
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
console.log(buf);
console.log(buf.readUIntBE(0, 3).toString(16));
console.log(buf.readUIntBE(1, 3).toString(16));
console.log(buf.readUIntBE(2, 2).toString(16));
|
Output:
<Buffer 21 09 19 98>
210919
91998
1998
Example 2:
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
console.log(buf);
console.log(buf.readUIntBE(0, 3).toString(16));
console.log(buf.readUIntLE(0, 3).toString(16));
console.log(buf.readUIntBE(1, 2).toString(16));
console.log(buf.readUIntLE(1, 2).toString(16));
console.log(buf.readUIntBE(2, 1).toString(16));
console.log(buf.readUIntLE(2, 1).toString(16));
|
Output:
<Buffer 21 09 19 98>
210919
190921
919
1909
19
19
Example 3:
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
console.log(buf);
console.log(buf.readUIntBE(0, 4).toString(16));
console.log(buf.readUIntBE(1, 2).toString(16));
console.log(buf.readUIntBE(2, 3).toString(16));
console.log(buf.readUIntBE(3, 6).toString(16));
|
Output:
<Buffer 21 09 19 98>
21091998
919
internal/buffer.js:49
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 <= 1. Received 2
at boundsError (internal/buffer.js:49:9)
at readUInt24BE (internal/buffer.js:205:5)
at Buffer.readUIntBE (internal/buffer.js:148:12)
. . .
Note: The above program will compile and run by using the node index.js
command.
Reference: https://nodejs.org/api/buffer.html#buffer_buf_readuintbe_offset_bytelength
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Oct, 2021
Like Article
Save Article