Skip to content

infinity.encoding

Provides access to various text encodings. infinity.encoding is both the enum of the supported encodings and a namespace with helper functions.

Module: none (built-in)

The module infinity.encoding (loaded with infinity.loadModule('infinity.encoding')) is not required for this namespace; it provides the namespaces infinity.base64, infinity.hex, infinity.quotedPrintable and infinity.url.

Functions:
Enums:

Functions

convert()

Reinterprets a string in another encoding: the string is converted to bytes using fromEncoding, and these bytes are then decoded using toEncoding. This is typically used to repair strings whose bytes were decoded with the wrong encoding (e.g. UTF-8 data that was read as ANSI).

Signature:

convert( value: string, fromEncoding: infinity.encoding, toEncoding: infinity.encoding ): string

Parameters:

  • value: string

    The string to be converted.

  • fromEncoding: infinity.encoding

    The encoding used to convert the string to bytes.

  • toEncoding: infinity.encoding

    The encoding used to decode the bytes into the resulting string.

Return type: string

Example:

//UTF-8 text that was wrongly decoded as Windows-1252
let wrongString = 'Grüße';
let convertedString = infinity.encoding.convert(wrongString, infinity.encoding.ansi, infinity.encoding.utf8);
console.log(convertedString);
//Grüße

toString()

Returns the charset name of the specified encoding, as used e.g. in HTTP headers: UTF-8 (utf8), WINDOWS-1252 (ansi), US-ASCII (ascii), UTF-7 (utf7), UTF-16LE (unicode), UTF-16BE (bigEndianUnicode). For systemDefault, the name of the encoding the system default maps to is returned (WINDOWS-1252 on Windows, UTF-8 on Linux and macOS). Note that ansi always returns WINDOWS-1252, regardless of the actual ANSI code page.

Signature:

toString( encoding: infinity.encoding ): string

Parameters:

Return type: string

Example:

console.log(infinity.encoding.toString(infinity.encoding.ansi));
//prints "WINDOWS-1252"

Enums

infinity.encoding

Values:

  • systemDefault: 0

    Default encoding, determined by the operating system: the ANSI code page on Windows, UTF-8 on Linux and macOS.

  • ansi: 1

    Windows-1252 on every platform (before 3.11, the system's ANSI code page: UTF-8 on Linux). Every byte value is mapped to a character and back, so binary data and text in other single-byte charsets survive reading and writing unchanged; characters that Windows-1252 cannot represent are written as ?.

  • ascii: 2

    ASCII encoding.

  • bigEndianUnicode: 3

    UTF-16 big endian encoding.

  • unicode: 4

    UTF-16 little endian encoding.

  • utf7: 5

    UTF-7 encoding.

  • utf8: 6

    UTF-8 encoding.

Example:

let encoding = infinity.encoding.systemDefault;