NAME
UTF-8 - ASCII 兼容的多字节 Unicode 编码
描述
The Unicode 字符集使用的是 16 位(双字节)码。最普遍的 Unicode 编码方法( UCS-2) 由一个 16 位双字序列组成。这样的字符串中包括了的一些如‘\0’或‘/’这样的在文件名中或者是在 C 库函数中具有特殊意义的字符。另外,如果没有做重大的修正的话,大部分操作 ASCII 码文件的 UNIX 工具不能够正确识别 16 位的字符。因此, UCS-2 对于 Unicode 的文件名、文本文件、环境变量等等来说并不是一种合适的外部编码方式。 ISO 10646 Universal Character Set (UCS), 是 Unicode 的超集,甚至使用了 31 位编码方式,另外还有使用 32 编码的 UCS-4 也有同样上述的问题。 UCS-4 而用 UTF-8 对 Unicode UCS 编码就不会存在这样的问题。所以,UTF-8 很明显的是在 UNIX 类操作系统下的 Unicode 字符集的解决方案。
属性
UTF-8 编码具有以下优良属性:
- *
- UCS 字符从 0x00000000 到 0x0000007f (传统的 US-ASCII 字符)简单地编码为字节 0x00 到 0x7f (与 ASCII 码兼容)。这意味着只包含 7 位 ASCII 字符的文件和字符串在 ASCII 和 UTF-8. 编码方式下是完全一样的。
- *
- 所有大于 0x7f 的 UCS 字符被编码成为多字节序列。该序列全部是由 0x80 到 0fd 的字符组成,这样就不会有标准 ASCII 字符会被作为某个字的一个部分这种现象出现,对于‘\0’和‘’这样的特殊字符来说也就不会有问题了。
- *
- 保留了 UCS-4 字典中的字节串的排列顺序。
- *
- 所有 2^32 次方的 UCS 码都能够使用 UTF-8 来进行编码。
- *
- 0xfe 和 0xff 两个字符在 UTF-8 中不会被用到。
- *
- 表示非 ASCII 码的 UCS 多字节串的开始字符总是 0xc0 到 0xfd 之间的字符,并会指出该串的长度。多字节串的其他字符都是 0x80 到 0xbf 之间的字符。这使得再同步非常简单,并令编码是无态的,丢字节现象也不容易发生。
- *
- 用 UTF-8 编码的 UCS 字符可以增加到 6 个字节的长度。而 Unicode 只能增加到 3 个字节长。由于 Linux 只使用 16 位的 Unicode , UCS 的子集。所以在 Linux 下, UTF-8 多字节串长度最多不会超过三个字节。
编码方式
下面的字节串用来表示一个字符。用什么串依照该字符在 UCS 编码中的序号来定:
- 0x00000000 - 0x0000007F:
- 0xxxxxxx
- 0x00000080 - 0x000007FF:
- 110xxxxx 10xxxxxx
- 0x00000800 - 0x0000FFFF:
- 1110xxxx 10xxxxxx 10xxxxxx
- 0x00010000 - 0x001FFFFF:
- 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x00200000 - 0x03FFFFFF:
- 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x04000000 - 0x7FFFFFFF:
- 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
这里 xxx 的位置二进制位形式的字符编码填入。只用最短的那个足够表达一个字符编码数的多字节串。
举例说明
Unicode 字符 0xa9 = 1010 1001 (版权所有的符号) 在 UTF-8 中被编码为:
- 11000010 10101001 = 0xc2 0xa9
字符0x2260 = 0010 0010 0110 0000 (“不等于”符号)被编码为:
- 11100010 10001001 10100000 = 0xe2 0x89 0xa0
遵循标准
ISO 10646, Unicode 1.1, XPG4, Plan 9.
作者
Markus Kuhn
参考
unicode(7)
#p#
NAME
UTF-8 - an ASCII compatible multi-byte Unicode encoding
DESCRIPTION
The Unicode 3.0 character set occupies a 16-bit code space. The most obvious Unicode encoding (known as UCS-2) consists of a sequence of 16-bit words. Such strings can contain as parts of many 16-bit characters bytes like '\0' or '/' which have a special meaning in filenames and other C library function parameters. In addition, the majority of UNIX tools expects ASCII files and can't read 16-bit words as characters without major modifications. For these reasons, UCS-2 is not a suitable external encoding of Unicode in filenames, text files, environment variables, etc. The ISO 10646 Universal Character Set (UCS), a superset of Unicode, occupies even a 31-bit code space and the obvious UCS-4 encoding for it (a sequence of 32-bit words) has the same problems.
The UTF-8 encoding of Unicode and UCS does not have these problems and is the common way in which Unicode is used on Unix-style operating systems.
PROPERTIES
The UTF-8 encoding has the following nice properties:
- *
- UCS characters 0x00000000 to 0x0000007f (the classic US-ASCII characters) are encoded simply as bytes 0x00 to 0x7f (ASCII compatibility). This means that files and strings which contain only 7-bit ASCII characters have the same encoding under both ASCII and UTF-8.
- *
- All UCS characters > 0x7f are encoded as a multi-byte sequence consisting only of bytes in the range 0x80 to 0xfd, so no ASCII byte can appear as part of another character and there are no problems with e.g. '\0' or '/'.
- *
- The lexicographic sorting order of UCS-4 strings is preserved.
- *
- All possible 2^31 UCS codes can be encoded using UTF-8.
- *
- The bytes 0xfe and 0xff are never used in the UTF-8 encoding.
- *
- The first byte of a multi-byte sequence which represents a single non-ASCII UCS character is always in the range 0xc0 to 0xfd and indicates how long this multi-byte sequence is. All further bytes in a multi-byte sequence are in the range 0x80 to 0xbf. This allows easy resynchronization and makes the encoding stateless and robust against missing bytes.
- *
- UTF-8 encoded UCS characters may be up to six bytes long, however the Unicode standard specifies no characters above 0x10ffff, so Unicode characters can only be up to four bytes long in UTF-8.
ENCODING
The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code number of the character:
- 0x00000000 - 0x0000007F:
- 0xxxxxxx
- 0x00000080 - 0x000007FF:
- 110xxxxx 10xxxxxx
- 0x00000800 - 0x0000FFFF:
- 1110xxxx 10xxxxxx 10xxxxxx
- 0x00010000 - 0x001FFFFF:
- 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x00200000 - 0x03FFFFFF:
- 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x04000000 - 0x7FFFFFFF:
- 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
The xxx bit positions are filled with the bits of the character code number in binary representation. Only the shortest possible multi-byte sequence which can represent the code number of the character can be used.
The UCS code values 0xd800-0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS non-characters) should not appear in conforming UTF-8 streams.
EXAMPLES
The Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded in UTF-8 as
- 11000010 10101001 = 0xc2 0xa9
and character 0x2260 = 0010 0010 0110 0000 (the "not equal" symbol) is encoded as:
- 11100010 10001001 10100000 = 0xe2 0x89 0xa0
APPLICATION NOTES
Users have to select a UTF-8 locale, for example with
- export LANG=en_GB.UTF-8
in order to activate the UTF-8 support in applications.
Application software that has to be aware of the used character encoding should always set the locale with for example
- setlocale(LC_CTYPE, "")
and programmers can then test the expression
- strcmp(nl_langinfo(CODESET), "UTF-8") == 0
to determine whether a UTF-8 locale has been selected and whether therefore all plaintext standard input and output, terminal communication, plaintext file content, filenames and environment variables are encoded in UTF-8.
Programmers accustomed to single-byte encodings such as US-ASCII or ISO 8859 have to be aware that two assumptions made so far are no longer valid in UTF-8 locales. Firstly, a single byte does not necessarily correspond any more to a single character. Secondly, since modern terminal emulators in UTF-8 mode also support Chinese, Japanese, and Korean double-width characters as well as non-spacing combining characters, outputting a single character does not necessarily advance the cursor by one position as it did in ASCII. Library functions such as mbsrtowcs(3) and wcswidth(3) should be used today to count characters and cursor positions.
The official ESC sequence to switch from an ISO 2022 encoding scheme (as used for instance by VT100 terminals) to UTF-8 is ESC % G ("\x1b%G"). The corresponding return sequence from UTF-8 to ISO 2022 is ESC % @ ("\x1b%@"). Other ISO 2022 sequences (such as for switching the G0 and G1 sets) are not applicable in UTF-8 mode.
It can be hoped that in the foreseeable future, UTF-8 will replace ASCII and ISO 8859 at all levels as the common character encoding on POSIX systems, leading to a significantly richer environment for handling plain text.
SECURITY
The Unicode and UCS standards require that producers of UTF-8 shall use the shortest form possible, e.g., producing a two-byte sequence with first byte 0xc0 is non-conforming. Unicode 3.1 has added the requirement that conforming programs must not accept non-shortest forms in their input. This is for security reasons: if user input is checked for possible security violations, a program might check only for the ASCII version of "/../" or ";" or NUL and overlook that there are many non-ASCII ways to represent these things in a non-shortest UTF-8 encoding.
STANDARDS
ISO/IEC 10646-1:2000, Unicode 3.1, RFC 2279, Plan 9.
AUTHOR
Markus Kuhn <mgk25@cl.cam.ac.uk>
SEE ALSO
nl_langinfo(3), setlocale(3), charsets(7), unicode(7)