![](https://csdnimg.cn/release/download_crawler_static/9991928/bgc.jpg)
Programming with Unicode Documentation, Release 2011
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 charac-
ters. Getting the n
th
character or the length in characters using UTF-16 has a complexity of 𝑂(𝑛), whereas it has a
complexity of 𝑂(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.
1
A UTF-8 encoder should not encode surrogate characters (U+D800—U+DFFF).
6 Chapter 3. Definitions