- 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.writeInt8() Method
The Buffer.writeInt8() method helps in writing a signed 8-bit integer of value at a given offset to the buffer. It returns undefined if the value is not an 8-bit signed integer.
Syntax
Following is the syntax of the Node.JS Buffer.writeInt8() Method −
buf.writeInt8(value[, offset])
Parameters
This method accepts two parameters. The same is explained below.
value − (required)A signed 8-bit number to be written to the buffer.
offset − The offset that indicates the position to start writing. 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
The method buffer.writeInt8() writes the given value and returns offset plus the number of bytes written.
Example
To create a buffer, we are going to make use of NodeJS Buffer.alloc() method −
const buffer = Buffer.alloc(10); buffer.writeInt8(8) console.log(buffer);
Output
On execution the value starting from 0th position will be written to the buffer created. The buffer length created for the above is 10. So we can only use the offset with values from 0 to 9. If any value >9 it will give an error ERR_OUT_OF_RANGE.
<Buffer 08 00 00 00 00 00 00 00 00 00>
Example
In this example will use buffer length as 5 and offset greater than buffer.length -4.
const buffer = Buffer.alloc(5); buffer.writeInt8(8, 5); console.log(buffer);
Output
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 5 at boundsError (internal/buffer.js:58:9) at write_Int8 (internal/buffer.js:575:5) at Buffer.writeUInt8 (internal/buffer.js:582:10) at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8) 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
Example
In this example will create a buffer and write from an offset that is within in range.
const buffer = Buffer.alloc(10); buffer.writeInt8(12, 4); console.log(buffer)
Output
The offset has to be between 0 to 9. The output of the above code, when executed, is as shown below −
<Buffer 00 00 00 00 0c 00 00 00 00 00>
To Continue Learning Please Login
Login with Google