XML technologies / XML / XML in a Nutshell / Internationalization / Unicode

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Unicode is an internationally standardized character set that lets you write documents in any language you will ever speak, learn or otherwise encounter, unless aliens abduct you. Version 4.0.1, current in June 2004, contains 96,447 characters from most living languages and some dead ones. (data2type editorial note: Version 7.0.0 from June 2014 contains 113,021 characters.) Unicode easily covers the Latin alphabet. It also includes Greek-derived scripts, such as ancient and modern Greek, and the Cyrillic scripts used in Serbia and much of the former Soviet Union. It covers various ideographic scripts, including Han characters used for Chinese and Japanese, Korean Hangul syllables, and phonetic representations of these languages, including Katakana and Hiragana. It covers right-to-left Arabic and Hebrew scripts, and scripts native to the Indian subcontinent, including Devanagari, Thai, Bengali, Tibetan and many others. This is less than half the scripts in Unicode 4.0. Probably fewer than one person in a thousand today speaks a language that Unicode cannot adequately represent. Future additions will reduce that small number further. Unicode can potentially hold over a million characters, but nobody will publicly admit where they think the remaining million will come from. After a few beers, some developers readily admit to preparing for the day we join an Intergalactic Federation of thousands of intelligent species.

The Unicode character set assigns characters to code points: numbers. These numbers can then be encoded using various schemes, including:

UCS-2 and UTF-16

UCS-2, also called ISO-10646-UCS-2, represents each character as a two-byte unsigned integer from 0 to 65,535. Thus uppercase A, Unicode code point 65, is represented by bytes 00 and 41 in hexadecimal. Uppercase B, code point 66, is represented by 00 and 42. Bytes 03 and A3 represent uppercase Greek Σ, code point 931.

UCS-2 comes in two varieties: MSB (Big Endian) and LSB (Little Endian). In MSB-UCS-2, the most significant byte comes first; LSB-UCS-2 reverses the order. In MSB-UCS-2, uppercase A is #x0041. (For reasons explained later, this book prefixes hexadecimal numbers with #x. Two hexadecimal digits make one byte.) In LSB-UCS-2, the bytes are reversed and A is #x4100. B is #x0042 in MSB-UCS-2 and #x4200 in LSB-UCS-2. Σ is #x03A3 in MSB-UCS-2 and #xA303 in LSB-UCS-2. We use MSB notation here, but parsers cannot assume it: they must determine byte order from the document itself.

To distinguish MSB-UCS-2 from LSB-UCS-2, a UCS-2 document always begins with Unicode character #xFEFF, a zero-width nonbreaking space. It is usually called a byte order mark (byte order mark). It has the advantage of being invisible. Reversing its bytes produces the nonexistent character #xFFFE. A program can therefore inspect a UCS-2 document’s first two bytes and immediately determine whether it uses MSB order, according to whether they are #xFEFF or #xFFFE.

UCS-2 nevertheless has three important drawbacks:

  • Files containing almost exclusively Latin text are nearly twice as large in UCS-2 as in a single-byte character set such as ASCII or Latin-1.

  • UCS-2 is neither backward nor forward compatible with ASCII. Tools designed for single-byte character sets often cannot process a UCS-2 file properly, even if it contains only ASCII characters. For example, a C program expecting a null byte to terminate strings will choke on a predominantly English UCS-2 file because almost every second byte is zero.

  • UCS-2 is limited to 65,536 characters.

The last problem matters less in practice: Unicode’s first 65,536 code points already meet most needs, except dead languages such as Ugaritic, fictional scripts such as Tengwar, and musical and some mathematical symbols. Unicode nevertheless offers a way to represent code points above 65,535 by interpreting certain two-byte sequences as halves of a surrogate pair. A Unicode document using UCS-2 plus surrogate pairs uses the so-called UTF-16 encoding.

The other two problems probably affect most developers. UTF-8 is an alternative Unicode encoding that solves both.

UTF-8

UTF-8 is a variable-length Unicode encoding. Characters 0 through 127, the ASCII character set, are encoded in one byte each, exactly as in ASCII. Byte 65 represents A in both ASCII and UTF-8. ASCII characters map one-to-one to UTF-8 bytes. Pure ASCII files are therefore also acceptable UTF-8 files.

UTF-8 uses two bytes for characters 128 through 2,047, covering most non-ideographic scripts. Characters 2,048 through 65,535, mainly Chinese, Japanese and Korean, use three bytes. Higher code points use four. For predominantly Latin files, this effectively halves size compared with UCS-2. Predominantly Japanese, Chinese or Indian-subcontinent languages can increase size by 50 percent. For most other living languages, size is roughly the same as UCS-2.

UTF-8 is probably the most widely supported Unicode encoding. For example, it is used by Java in its .class files to store strings, is BeOS’s native encoding, and is the default encoding assumed by an XML processor unless a byte order mark or encoding declaration specifies otherwise. When software says it stores Unicode, it is quite likely using UTF-8.

  

 

 

 

back to top