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.
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:
stringThe string to be converted.
-
fromEncoding:
infinity.encodingThe encoding used to convert the string to bytes.
-
toEncoding:
infinity.encodingThe 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:
- encoding:
infinity.encodingThe encoding. Throws an exception for invalid values.
Return type: string
Example:
console.log(infinity.encoding.toString(infinity.encoding.ansi));
//prints "WINDOWS-1252"
infinity.encoding¶
Values:
-
systemDefault:
0Default encoding, determined by the operating system: the ANSI code page on Windows, UTF-8 on Linux and macOS.
-
ansi:
1Windows-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:
2ASCII encoding.
-
bigEndianUnicode:
3UTF-16 big endian encoding.
-
unicode:
4UTF-16 little endian encoding.
-
utf7:
5UTF-7 encoding.
-
utf8:
6UTF-8 encoding.
Example:
let encoding = infinity.encoding.systemDefault;