Open Source Cross-Platform Game Programming

TextEncoding

Encodes text into bytes and decodes bytes into text. Supports ASCII, ISO-8859-1, UTF8, UTF16, and UTF32 and endian/BOM variations.

enum TextFormat[link]

Enum list of available encodings. Endian and BOM variations are passed in as separate parameters in places where these are used.

NameDescription
UNKNOWN

Encoding is unknown. Used when decoding, but not for encoding.

ASCII

The ASCII format (single-byte encoding with 7-bit values)

ISO_8859_1

The ISO-8859-1 encoding (single-byte encoding with 8-bit values)

UTF-8

The UTF-8 format. (variable-length encoding format. Characters are 1-4 bytes. Generally considered standard.

UTF-16

The UTF-16 format. (2-byte format with surrogate pairs)

UTF-32

The UTF-32 format. (4-byte format, each 4-bit integer is the Unicode code point)

function convertBytesToText(bytes, encoding)[link]

Converts a list of bytes into a string.

Arguments

NameTypeOptionalDescription
bytes list of integers

A list of byte values from 0-255

encoding TextEncoding.TextFormat Optional

An encoding to decode the bytes. If not specified, auto-detection will be attempted.

Return Value

TypeDescription
string

The string of the decoded bytes.

function convertTextToBytes(value, encoding, includeBom)[link]

Converts a string into a byte list.

Arguments

NameTypeOptionalDescription
value string

A string to encode into bytes.

encoding TextEncoding.TextFormat

The encoding to use for the conversion.

includeBom boolean Optional

Whether or not to include a BOM at the beginning of the byte list. Ignored if the format does not support a BOM. Default value is false when not specified.

Return Value

TypeDescription
list of integers

A list of byte values from 0 to 255.

class EncodingException[link]

Thrown when there is a problem performing a conversion.