Node.js Buffer.readInt8() Method
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:
// Node.js program to demonstrate the // buffer.readUInt8() method const value = Buffer.from([ -2, 3 ]); // Reads the first value console.log(value.readInt8(0)); // Reads the second value console.log(value.readInt8(1)); // Throws an error console.log(value.readInt8(2)); |
Output:
-2 3 RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
Example 2
// Node.js program to demonstrate the // buffer.readUInt8() method const obj = Buffer.from([ 0X52, 0X40, 0X78 ]); // It returns the first value console.log(obj.readUInt8(0)); // It returns the third value console.log(obj.readUInt8(2)); const temp = Buffer.from( "XYZ" ); // It returns the ASCII value of capital 'X' 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
Contact Us