function

buffer.transcode

function transcode(
source: Uint8Array,
): NonSharedBuffer;

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.

@param source

A Buffer or Uint8Array instance.

@param fromEnc

The current encoding.

@param toEnc

To target encoding.

Referenced types

class Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>

A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.

  • readonly [Symbol.toStringTag]: 'Uint8Array'
  • readonly buffer: TArrayBuffer

    The ArrayBuffer instance referenced by the array.

  • readonly byteLength: number

    The length in bytes of the array.

  • readonly byteOffset: number

    The offset in bytes of the array.

  • readonly BYTES_PER_ELEMENT: number

    The size in bytes of each element in the array.

  • readonly length: number

    The length of the array.

  • [Symbol.iterator](): ArrayIterator<number>;
  • index: number
    ): undefined | number;

    Returns the item located at the specified index.

    @param index

    The zero-based index of the desired code unit. A negative index will count back from the last item.

  • target: number,
    start: number,
    end?: number
    ): this;

    Returns the this object after copying a section of the array identified by start and end to the same array starting at position target

    @param target

    If target is negative, it is treated as length+target where length is the length of the array.

    @param start

    If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

    @param end

    If not specified, length of the this object is used as its default value.

  • entries(): ArrayIterator<[number, number]>;

    Returns an array of key, value pairs for every entry in the array

  • predicate: (value: number, index: number, array: this) => unknown,
    thisArg?: any
    ): boolean;

    Determines whether all the members of an array satisfy the specified test.

    @param predicate

    A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

    @param thisArg

    An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • value: number,
    start?: number,
    end?: number
    ): this;

    Changes all array elements from start to end index to a static value and returns the modified array

    @param value

    value to fill array section with

    @param start

    index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.

    @param end

    index to stop filling the array at. If end is negative, it is treated as length+end.

  • predicate: (value: number, index: