Skip to main content
Buffer.swap16 - node__buffer.d.ts - Node documentation
method Buffer.swap16

Usage in Deno

```typescript import { type Buffer } from "node:node__buffer.d.ts"; ```
Buffer.swap16(): this
Interprets `buf` as an array of unsigned 16-bit integers and swaps the byte order _in-place_. Throws `ERR_INVALID_BUFFER_SIZE` if `buf.length` is not a multiple of 2. ```js import { Buffer } from 'node:buffer'; const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); console.log(buf1); // Prints: buf1.swap16(); console.log(buf1); // Prints: const buf2 = Buffer.from([0x1, 0x2, 0x3]); buf2.swap16(); // Throws ERR_INVALID_BUFFER_SIZE. ``` One convenient use of `buf.swap16()` is to perform a fast in-place conversion between UTF-16 little-endian and UTF-16 big-endian: ```js import { Buffer } from 'node:buffer'; const buf = Buffer.from('This is little-endian UTF-16', 'utf16le'); buf.swap16(); // Convert to big-endian UTF-16 text. ```

Return Type

this
A reference to `buf`.