3. Definitions

3.1. Character

3.2. Glyph

3.3. Code point

A code point is an unsigned integer. The smallest code point is zero. Code points are usually written as hexadecimal, e.g. “0x20AC” (8,364 in decimal).

3.4. Character set (charset)

A character set, abbreviated charset, is a mapping between code points and characters. The mapping has a fixed size. For example, most 7 bits encodings have 128 entries, and most 8 bits encodings have 256 entries. The biggest charset is the Unicode Character Set 6.0 with 1,114,112 entries.

In some charsets, code points are not all contiguous. For example, the cp1252 charset maps code points from 0 though 255, but it has only 251 entries: 0x81, 0x8D, 0x8F, 0x90 and 0x9D code points are not assigned.

Examples of the ASCII charset: the digit five (“5”, U+0035) is assigned to the code point 0x35 (53 in decimal), and the uppercase letter “A” (U+0041) to the code point 0x41 (65).

The biggest code point depends on the size of the charset. For example, the biggest code point of the ASCII charset is 127 (2^7-1)

Charset examples:

Charset Code point Character
ASCII 0x35 5 (U+0035)
ASCII 0x41 A (U+0041)
ISO-8859-15 0xA4 € (U+20AC)
Unicode Character Set 0x20AC € (U+20AC)

3.5. Character string

A character string, or “Unicode string”, is a string where each unit is a character. Depending on the implementation, each character can be any Unicode character, or only characters in the range U+0000—U+FFFF, range called the Basic Multilingual Plane (BMP). There are 3 different implementations of character strings:

  • array of 32 bits unsigned integers (the UCS-4 encoding): full Unicode range
  • array of 16 bits unsigned integers (UCS-2): BMP only
  • array of 16 bits unsigned integers with surrogate pairs (UTF-16): full Unicode range

UCS-4 use twice as much memory than UCS-2, but it supports all Unicode character. UTF-16 is a compromise between UCS-2 and UCS-4: characters in the BMP range use one UTF-16 unit (16 bits), characters outside this range use two UTF-16 units (a surrogate pair, 32 bits). This advantage is also the main disadvantage of this kind of character string.

The length of a character string implemented using UTF-16 is the number of UTF-16 units, and not the number of characters, which is confusing. For example, the U+10FFFF character is encoded as two UTF-16 units: {U+DBFF, U+DFFF}. If the character string only contains characters of the BMP range, the length is the number of characters. Getting the nth character or the length in characters using UTF-16 has a complexity of O(n), whereas it has a complexity of O(1) for UCS-2 and UCS-4 strings.

The Java language, the Qt library and Windows 2000 implement character strings with UTF-16. The C and Python languages use UTF-16 or UCS-4 depending on: the size of the wchar_t type (16 or 32 bits) for C, and the compilation mode (narrow or wide) for Python. Windows 95 uses UCS-2 strings.

See also

UCS-2, UCS-4 and UTF-16 encodings, and surrogate pairs.

3.6. Byte string

A byte string is a character string encoded to an encoding. It is implemented as an array of 8 bits unsigned integers. It can be called by its encoding. For example, a byte string encoded to ASCII is called an “ASCII encoded string”, or simply an “ASCII string”.

The character range supported by a byte string depends on its encoding, because an encoding is associated to a charset. For example, an ASCII string can only store characters in the range U+0000—U+007F.

The encoding is not stored explicitly in a byte string. If the encoding is not documented or attached to the byte string, the encoding has to be guessed, which is a difficult task. If a byte string is decoded from the wrong encoding, it will not be displayed correctly, leading to a well known issue: mojibake.

The same problem occurs if two byte strings encoded to different encodings are concatenated. Never concatenate byte strings encoded to different encodings! Use character strings, instead of byte strings, to avoid mojibake issues.

PHP5 only supports byte strings. In the C language, “strings” are usually byte strings which are implemented as the char* type (or const char*).

See also

The char* type of the C language and the mojibake issue.

3.7. UTF-8 encoded strings and UTF-16 character strings

A UTF-8 string is a particular case, because UTF-8 is able to encode all Unicode characters [1] . But a UTF-8 string is not a Unicode string because the string unit is byte and not character: you can get an individual byte of a multibyte character.

Another difference between UTF-8 strings and Unicode strings is the complexity of getting the nth character: O(n) for the byte string and O(1) for the Unicode string. There is one exception: if the Unicode string is implemented using UTF-16: it has also a complexity of O(n).

[1]A UTF-8 encoder should not encode surrogate characters (U+D800—U+DFFF).

3.8. Encoding

An encoding describes how to encode code points to bytes and how to decode bytes to code points.

An encoding is always associated to a charset. For example, the UTF-8 encoding is associated to the Unicode charset. So we can say that an encoding encodes characters to bytes and decode bytes to characters, or more generally, it encodes a character string to a byte string and decodes a byte string to a character string.

The 7 and 8 bits charsets have most simple encoding: store a code point as a single byte. These charsets are also called encodings, it is easy to confuse them. The best example is the ISO-8859-1 encoding: all of the 256 possible bytes are considered as 8 bit code points (0 through 255) and are associated to characters. For example, the character A (U+0041) has the code point 65 (0x41 in hexadecimal) and is stored as the byte 0x41.

Charsets with more than 256 entries cannot encode all code points into a single byte. The encoding encode all code points into byte sequences of the same length or of variable length. For example, UTF-8 is a variable length encoding: code points lower than 128 use a single byte, whereas higher code points take 2, 3 or 4 bytes. The UCS-2 encoding encodes all code points into sequences of two bytes (16 bits).

3.9. Encode a character string

Encode a character string to a byte string, to an encoding. For example, encode “Hé” to UTF-8 gives 0x48 0xC3 0xA9.

By default, most libraries are strict: raise an error at the first unencodable character. Some libraries allow to choose how to handle them.

Most encodings are stateless, but some encoding requires a stateful encoder. For example, the UTF-16 encoding starts by generating a BOM, 0xFF 0xFE or 0xFE 0xFF depending on the endian.

3.10. Decode a byte string

Decode a byte string from an encoding to a character string. For example, decode 0x48 0xC3 0xA9 from UTF-8 gives “Hé”.

By default, most libraries raise an error if a byte sequence cannot be decoded. Some libraries allow to choose how to handle them.

Most encodings are stateless, but some encoding requires a stateful decoder. For example, the UTF-16 encoding decodes the two first bytes as a BOM to read the endian (use UTF-16-LE or UTF-16-BE).

3.11. Mojibake

When a byte strings is decoded from the wrong encoding, or when two byte strings encoded to different encodings are concatenated, a program will display mojibake.

The classical example is a latin string (with diacritics) encoded to UTF-8 but decoded from ISO-8859-1. It displays é {U+00C3, U+00A9} for the é (U+00E9) letter, because é is encoded to 0xC3 0xA9 in UTF-8.

Other examples:

Text Encoded to Decoded from Result
Noël UTF-8 ISO-8859-1 Noël
Русский KOI-8 ISO-8859-1 òÕÓÓËÉÊ

Note

“Mojibake” is japanese word meaning literally “unintelligible sequence of characters”. This issue is called “Кракозя́бры” (krakozyabry) in Russian.

3.12. Unicode: an Universal Character Set (UCS)

See also

UCS-2, UCS-4, UTF-8, UTF-16, and UTF-32 encodings.