Variable-width encoding: Difference between revisions

Content deleted Content added
make more metion of when stuff is in hex
Line 10:
For example, the four character string "I{{unicode|&#9829;}}NY" is encoded in [[UTF-8]] like this (shown as [[hexadecimal]] byte values): <span style="color:green">49</span> <span style="color:red">E2</span> <span style="color:blue">99</span> <span style="color:blue">A5</span> <span style="color:green">4E</span> <span style="color:green">59</span>. Of the six units in that sequence, <span style="color:green">49</span>, <span style="color:green">4E</span>, and <span style="color:green">59</span> are singletons (for ''I, N,'' and ''Y''), <span style="color:red">E2</span> is a lead unit and <span style="color:blue">99</span> and <span style="color:blue">A5</span> are trail units. The heart symbol is represented by the combination of the lead unit and the two trail units.
 
UTF-8 is one of the best-designed multibyte encodings because the three sorts of units are kept apart and are easy for a program to identify. Older variable-width encodings are typically not so well designed, and in them the trail and lead units may use the same values, and in some all three sorts use overlapping values. Where there is overlap, a text processing application that deals with the variable-width encoding must scan the text from the beginning of all definitive sequences in order to identify the various units properly and interpret the text correctly. In such encodings, one is liable to encounter false positives when searching for a string in the middle of the text. For example, if the hexadecimal values DE and DF and E0 and E1 can all be either lead units or trail units, then a search for the two-unit sequence DF E0 can yield a false positive in the two consecutive two-unit sequences DE DF E0 E1. There is also the danger that a single corrupted or lost unit may render the whole interpretation of a large run of multiunit sequences totally differently. In a variable-width encoding where all three sorts of units are disjunct, string searching always works without false positives, and the corruption of one unit corrupts only one character.
 
==[[CJK]] multibyte encodings==
 
The first use of multibyte encodings was for the encoding of Chinese, Japanese and Korean, which have large character sets well in excess of 256 characters. At first the encoding was constrained to the limit of 7 bits. The ISO-2022-JP, ISO-2022-CN and ISO-2022-KR encodings used the range 21-7E (hexadecimal) for both lead units and trail units, and marked them off from the singletons by using ISO&#160;2022 escape sequences to switch between single-byte and multibyte mode. A total of 8,836 (94&#215;94) characters could be encoded at first, and further sets of 94&#215;94 characters with switching. The ISO&#160;2022 encoding schemes for CJK are still in use on the Internet. The statefull nature of theese encodings and the large overlap makes them very awkward to process.
 
On [[Unix]] platforms, the ISO&#160;2022 7-bit encodings were replaced by a set of 8-bit encoding schemes, the Extended Unix Code: EUC-JP, EUC-CN and EUC-KR. Instead of distinguishing between the multiunit sequences and the singletons with escape sequences, which made the encodings stateful, multiunit sequences were marked by having the most significant bit set, that is, being in the range 80-FF (hexadeximal), while the singletons were in the range 00-7F alone. The lead units and trail units were in the range A1 to FE (hexadeximal), that is, the same as their range in the ISO&#160;2022 encodings, but with the high bit set to 1. Theese encodings are reasonablly easy to work with provided you were only interested in searching and cutting at ascii characters but a break in the middle of a multi-byte character could still cause major curruption.
 
On the PC ([[MS-DOS]] and [[Microsoft Windows]] platforms), two encodings became established for Japanese and Traditional Chinese in which all of singletons, lead units and trail units overlapped: [[Shift-JIS]] and [[Big5]] respectively. In Shift-JIS, lead units had the range 81-9F and E0-FC, trail units had the range 40-7E and 80-FC, and singletons had the range 21-7E and A1-DF. In Big5, lead units had the range A1-FE, trail units had the range 40-7E and A1-FE, and singletons had the range 21-7E (all values in hexadecimal). This overlap again made processing tricky though at least most of the symbols have unique byte values (though strangely the backslash does not).<!--FIXME: GBK and code page 949 should probabbly also be mentioned here-->
 
==Unicode variable-width encodings==