function transcode
Usage in Deno
import { transcode } from "node:buffer";
transcode(): Buffer
Re-encodes the given Buffer
or Uint8Array
instance from one character
encoding to another. Returns a new Buffer
instance.
Throws if the fromEnc
or toEnc
specify invalid character encodings or if
conversion from fromEnc
to toEnc
is not permitted.
Encodings supported by buffer.transcode()
are: 'ascii'
, 'utf8'
, 'utf16le'
, 'ucs2'
, 'latin1'
, and 'binary'
.
The transcoding process will use substitution characters if a given byte sequence cannot be adequately represented in the target encoding. For instance:
import { Buffer, transcode } from 'node:buffer'; const newBuf = transcode(Buffer.from('€'), 'utf8', 'ascii'); console.log(newBuf.toString('ascii')); // Prints: '?'
Because the Euro (€
) sign is not representable in US-ASCII, it is replaced
with ?
in the transcoded Buffer
.
fromEnc: TranscodeEncoding
The current encoding.
toEnc: TranscodeEncoding
To target encoding.