- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - Buffer.readUInt8() Method
The Node.JS Buffer.readUInt8() method helps to read a 8-bit unsigned number at a given offset from the current buffer object. With 8-bit the range of value for unsigned is 0 to 255. For signed it is -128 to 127.
Syntax
Following is the syntax of the Node.JS Buffer.readUInt8() Method −
buf.readUInt8([offset])
Parameters
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-1. The default value is 0.
Return value
This method returns the 8-bit unsigned integer number from 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([1, 2 ,3, 4, 5, 6, 7, 8]); console.log("buffer length is ", buffer.length); console.log("Reading integer at offset 5:", buffer.readUInt8(5));
Output
The offset that can be used is length of buffer −1. So in the above example we can use 0 to 7. We have used offset as 5 so an 8-bit unsigned integer at the 5th position will be returned. If the offset given is greater than 7 it will give an error ERR_OUT_OF_RANGE.
buffer length is 8 Reading integer at offset 5: 6
Example
In the example below will make use of the string in buffer.from().
const buffer = Buffer.from("Hello World"); console.log("buffer length is ", buffer.length); console.log("Reading integer at offset 2:", buffer.readUInt8(2));
Output
In the above example, the length of the buffer created is 11. So for offset, we can use the value from 0 to 10.
buffer length is 11 Reading integer at offset 2: 108
Example
This example will check on the error if the offset is greater than the buffer.length-1. Let us create a buffer having a length of 8 and with that, you can offset the value as 0 to 7.
const buffer = Buffer.from([1, 2 ,3, 4, 5, 6, 7, 8]); console.log("buffer length is ", buffer.length); console.log("Reading at big integer at offset 8:", buffer.readUInt8(8));
Output
Since we have used an offset greater than 7 in the above program, it will throw an error as shown below −
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 <= 7. Received 8 at boundsError (internal/buffer.js:58:9) at Buffer.readUInt8 (internal/buffer.js:148: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
To Continue Learning Please Login
Login with Google