The Buffer.readUInt8() method is used to read an unsigned 8-bit integer from a Buffer from a with the specific offset.
Syntax:
Buffer.readInt8( offset )
Parameters: This method accepts single parameter offset which represents the number of bytes to skip before starting to read. The value of offset lies between the range 0 to buffer.length – 1. Its default value is 0.
Return Value: This method returns the 8-bit signed integer value at specified offset.
Example 1:
const value = Buffer.from([ -2, 3 ]);
console.log(value.readInt8(0));
console.log(value.readInt8(1));
console.log(value.readInt8(2));
|
Output:
-2
3
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
Example 2
const obj = Buffer.from([ 0X52, 0X40, 0X78 ]);
console.log(obj.readUInt8(0));
console.log(obj.readUInt8(2));
const temp = Buffer.from( "XYZ" );
console.log(temp.readUInt8(0));
|
Output:
82
120
88
Note: Integers that reads from the Buffer are illustrated as two’s complement signed values.
Reference: https://nodejs.org/api/buffer.html#buffer_buf_readint8_offset