- 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.swap16(), swap32(), swap64() Method
The Buffer.swap16(), Buffer.swap32(), Buffer.swap64() method considers the buffer as an array of unsigned 16 or 32 or 64 bit integers and swaps the byte order in-place and returns a reference to the buffer.
Please note for −
Buffer.swap16(), it will throw an error called ERR_INVALID_BUFFER_SIZE if the buffer length is not a multiple of 2.
Buffer.swap32(), it will throw an error called ERR_INVALID_BUFFER_SIZE if the buffer length is not a multiple of 4.
Buffer.swap64(), it will throw an error called ERR_INVALID_BUFFER_SIZE if the buffer length is not a multiple of 8.
Syntax
Following is the syntax of the Node.JS Buffer.swap16(), Buffer.swap32(), Buffer.swap64() Method −
buf.swap16() buf.swap32() buf.swap64()
Parameters
This method does not accept any parameters.
Return value
The method buffer.swap16(), buffer.swap32(), buffer.swap64() returns a reference to the buffer used by swapping the byte order in-place.
Example
To create a buffer, we are going to make use of NodeJS Buffer.from() method −
const buf1 = Buffer.from([11, 12, 13, 14, 15, 16]); console.log(buf1); const buf2 = buf1.swap16(); console.log(buf2);
Output
In above we have used NodeJS buf.swap16() and the same returns a reference to the buffer used.
<Buffer 0b 0c 0d 0e 0f 10> <Buffer 0c 0b 0e 0d 10 0f>
Example
To create a buffer, we are going to make use of NodeJS Buffer.from() method −
const buf1 = Buffer.from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]); console.log(buf1); const buf2 = buf1.swap32(); console.log(buf2);
Output
In above we have used NodeJS buf.swap32() and the same returns a reference to the buffer used.
<Buffer 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10> <Buffer 04 03 02 01 08 07 06 05 0c 0b 0a 09 10 0f 0e 0d>
Example
To create a buffer, we are going to make use of Buffer.from() method −
const buf1 = Buffer.from([1,2,3,4,5,6,7,8]); console.log(buf1); const buf2 = buf1.swap64(); console.log(buf2);
Output
In above we have used NodeJS buf.swap64() and the same returns a reference to the buffer used.
<Buffer 01 02 03 04 05 06 07 08> <Buffer 08 07 06 05 04 03 02 01>
To Continue Learning Please Login
Login with Google