Only try to delete address if present.
[gedcom-parse.git] / doc / encoding.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Character encoding</title>
2   
3   <meta http-equiv="content-type" content="text/html; 
4 charset=UTF-8"></head><body>
5 <h1 align="center">Character encoding<br>
6 </h1>
7
8 <h2>Index</h2>
9
10 <ul>
11
12   <li><a href="#The_character_encoding_problem">The character encoding problem</a></li>
13   <li><a href="#Unicode">Unicode code points</a></li><li><a href="#Unicode_encodings_UTF-8">Unicode encodings, UTF-8</a></li></ul><br>
14
15 <hr width="100%" size="2">
16 <h2><a name="The_character_encoding_problem"></a>The character encoding problem</h2>
17
18
19 Developers are usually familiar with the ASCII character set.  This
20 is a character set that assigns a unique number to some characters, e.g.
21 an "A" has ASCII code 65 (or 0x41 in hex), and an "a" has ASCII code 97 (or
22 0x61 in hex).  Some people may also have used ASCII codes for several
23 drawing characters (such as a horizontal bar, a vertical bar, or a top-right
24 corner) in the old DOS days, to be able to draw nice windows in text mode.<br>
25
26 <br>
27
28
29 However, these last characters are strictly spoken not part of the ASCII
30 set.  The standard ASCII set contains only the character positions from
31 0 to 127 (i.e. anything that fits into an integer that is 7 bits wide).  An
32 example of this table can be found <a href="http://web.cs.mun.ca/%7Emichael/c/ascii-table.html">here</a>.  Anything that has an ASCII code between 128 and 255 is in principle undefined.<br>
33
34 <br>
35
36
37 Now, several systems (including the old DOS) have defined those character
38 positions anyway, but usually in totally different ways.  Some well
39 known extensions are:<br>
40
41 <ul>
42
43 <li>the <a href="http://czyborra.com/charsets/cp437.gif">DOS</a>
44  character set, nowadays usually known as Code Page 437, but sometimes also
45 named LatinUS, ECS (Extended Character Set) or PC-8; note that the table
46 displayed in the link also contains the standard ASCII part</li><li>the <a href="http://czyborra.com/charsets/cp1252.gif">ANSI</a> character set, also known as Code Page 1252, and usually the default on Windows</li><li>the <a href="http://czyborra.com/charsets/iso8859-1.gif">ISO-8859-1</a> character set (also called Latin-1), which is an ISO standard for Western European languages, mostly used on various Unices</li><li>the <a href="http://czyborra.com/charsets/adobe-stdenc.gif">Adobe Standard Encoding</a>, which is by default used in Postscript, unless overridden</li>
47 </ul>
48
49
50 And these are only examples of character sets used in West-European languages.
51  For Japanese, Chinese, Korean, Vietnamese, ... there are separate character
52 sets in which one byte's meaning can even be influenced by what the previous
53 byte was, i.e. these are multi-byte character sets.  This is because
54 even 256 characters (the maximum for 8 bits) is totally inadequate to represent all characters in
55 such languages.<br>
56
57 <br>
58
59
60 So, summarizing, if a text file contains a byte that has a value 65, it is
61 pretty safe to assume that this byte represents an "A", if we ignore the
62 multi-byte character sets spoken of before.  However, a value 233 cannot
63 be interpreted without knowing in which character set the text file is written.
64  In Latin-1, it happens to be the character "é", but in another
65 character set it can be something totally different (e.g. in the DOS character
66 set it is the Greek letter theta).<br>
67
68 <br>
69
70 <div align="center"><img src="images/fromhex.png" alt="Conversion from byte value" width="386" height="140">
71 <br>
72 </div>
73
74
75 <br>
76
77
78 Vice versa, if you need to write a character "é" to a file, it depends
79 on the character set you will use what the numerical value will be in the
80 file: in Latin-1 it will be 233, but if you use the DOS character set it
81 will be 130, making it necessary again to know the encoding when you want to re-read the file.<br>
82
83 <br>
84
85 <div align="center"><img src="images/tohex.png" alt="Conversion to byte value" width="382" height="140">
86 <br>
87 <br>
88 <div align="left">This is a source of great confusion as soon as you go outside
89 the normal English character set, especially when you are using files on
90 different systems...<br>
91 </div>
92 </div>
93
94
95 <hr width="100%" size="2">
96 <h2><a name="Unicode"></a>Unicode code points</h2>
97
98
99 Enter the <a href="www.unicode.org">Unicode</a> standard...<br>
100
101 <br>
102
103 Unicode solves the problem of encoding by assigning unique numbers to <b>every</b>
104  character that is used anywhere in the world.  Since it is not possible
105 to do this in 8 bits (with a maximum of 256 code positions), a Unicode character
106 is usually represented by 16 bits, denoted by U+0000 to U+FFFF in hexadecimal
107 style.  A number such as U+0123 is named a "code point".<br>
108
109 <br>
110  Recently (Unicode 3.1), some extensions have even been defined so that in 
111 fact the defined range is now U+000000 to U+10FFFF (21 bits), and formally,
112 the character set is defined as 31-bits to allow for future expansion.<br>
113
114 <br>
115
116 The Unicode character set is backward compatible with the ISO-8859-1 or Latin-1
117 character set (and thus automatically also with the ASCII character set),
118 because for every ISO-8859-1 character with hexadecimal value 0xXY, the corresponding
119 Unicode code point is U+00XY.<br>
120
121 <br>
122  Some examples of Unicode code points (some of the characters here may not
123 be displayed correctly in all browsers; current Mozilla works perfectly for
124 this, but it also depends on the installed fonts of course):<br>
125 <br>
126
127 <table cellpadding="2" cellspacing="2" border="1" width="50%" align="center">
128
129   <tbody>
130     <tr>
131       <td valign="top" align="center"><b>Unicode code point</b><br>
132       </td>
133       <td valign="top" align="center"><b>Character</b><br>
134       </td>
135     </tr>
136     <tr>
137       <td valign="top">U+0041<br>
138       </td>
139       <td valign="top">A<br>
140       </td>
141     </tr>
142     <tr>
143       <td valign="top">U+00E9<br>
144       </td>
145       <td valign="top">é<br>
146       </td>
147     </tr>
148     <tr>
149       <td valign="top">U+03B8<br>
150       </td>
151       <td valign="top">θ (the Greek theta)<br>
152       </td>
153     </tr>
154     <tr>
155       <td valign="top">U+20AC<br>
156       </td>
157       <td valign="top">€ (the euro)<br>
158       </td>
159     </tr>
160   </tbody>
161 </table>
162
163 <br>
164 Using the Unicode code points there is no confusion anymore which character
165 is meant, because they uniquely define the character.  The full Unicode
166 code charts can be found <a href="http://www.unicode.org/charts">here</a>
167  (as a set of PDF documents).  A nice application to see all Unicode
168 characters is the Unicode Character Map (ucm), which can be found <a href="ftp://ftp.dcs.ed.ac.uk/pub/jec/programs/">here</a>, and which allows to select and paste any Unicode character.<br>
169 <br>
170 Some additional terminology (more terminology follows in the next section):<br>
171 <ul>
172   <li><b>UCS</b> (Universal Character Set): the official name for the Unicode character set<br>
173     <br>
174   </li>
175   <li><b>ISO 10646</b>: the international standard that defines the Unicode character set<br>
176     <br>
177   </li>
178   <li><b>BMP</b>
179  (Basic Multilingual Plane) or Plane 0 is the 16-bit subset of UCS, i.e.
180 the characters U+0000 to U+FFFF, which is supposed to cover all characters
181 is all currently used languages.  Code points outside that range are used
182 for historical character sets (e.g. hieroglyphs) and special symbols.</li>
183 </ul>
184 <hr width="100%" size="2">
185 <h2><a name="Unicode_encodings_UTF-8"></a>Unicode encodings, UTF-8</h2>
186 Since Unicode characters are generally represented by a number that is 16
187 bits wide, as seen above (for the basic plane), it would seem that all text
188 files would double in size, since the usual ASCII characters are 8 bits wide.
189  However, the Unicode code points are not necessarily the values that
190 are written to files...  <br>
191 <br>
192 Indeed, the simplest solution is to take the code point that defines a character,
193 split it up into two bytes, and write the two bytes to the file.  This
194 is called the UCS-2 encoding scheme:<br>
195 <br>
196 <table cellpadding="2" cellspacing="2" border="1" width="75%" align="center">
197   <tbody>
198     <tr>
199       <td valign="top" align="center"><b>Character</b><br>
200       </td>
201       <td valign="top" align="center"><b>Unicode code point</b><br>
202       </td>
203       <td valign="top" align="center"><b>Byte values in file (UCS-2)</b><br>
204       </td>
205     </tr>
206     <tr>
207       <td valign="top">A<br>
208       </td>
209       <td valign="top">U+0041<br>
210       </td>
211       <td valign="top">0x00, 0x41<br>
212       </td>
213     </tr>
214     <tr>
215       <td valign="top">é<br>
216       </td>
217       <td valign="top">U+00E9<br>
218       </td>
219       <td valign="top">0x00, 0xE9<br>
220       </td>
221     </tr>
222     <tr>
223       <td valign="top">θ (theta)<br>
224       </td>
225       <td valign="top">U+03B8<br>
226       </td>
227       <td valign="top">0x03, 0xB8<br>
228       </td>
229     </tr>
230     <tr>
231       <td valign="top">€ (euro)<br>
232       </td>
233       <td valign="top">U+20AC<br>
234       </td>
235       <td valign="top">0x20, 0xAC<br>
236       </td>
237     </tr>
238   </tbody>
239 </table>
240 <br>
241 This table assumes a big-endian encoding of UCS-2: the endianness is in principle
242 not defined, so there are two versions of UCS-2.  The little-endian
243 encoding results in the same values as in the table above, but in the inverse
244 order.<br>
245 <br>
246 So, we see that the UCS-2 encoding results in a doubling of file sizes for
247 files that contain only English text.  This is a disadvantage for this
248 encoding.  Another disadvantage is that null bytes can occur in normal
249 text, breaking all conventions for null-terminated C strings if you use the
250 normal <code>char</code> type.  This is why C also defines the <code>wchar_t</code>
251  type, which can hold a 32-bit character (at least in GNU systems).  To
252 avoid both of these disadvantages, UTF-8 was introduced.<br>
253 <br>
254 In UTF-8, the number of bytes used to write a character to a file depends
255 on the Unicode code point.  The corresponding table to the table above
256 is:<br>
257 <br>
258 <table cellpadding="2" cellspacing="2" border="1" width="75%" align="center">
259 <tbody><tr><td valign="top" align="center"><b>Character</b><br></td><td valign="top" align="center"><b>Unicode code point</b><br></td><td valign="top" align="center"><b>Byte values in file (UTF-8)</b><br></td></tr><tr><td valign="top">A<br></td><td valign="top">U+0041<br></td><td valign="top">0x41<br></td></tr><tr><td valign="top">é<br></td><td valign="top">U+00E9<br></td><td valign="top">0xC3, 0xA9<br></td></tr><tr><td valign="top">θ (theta)<br></td><td valign="top">U+03B8<br></td><td valign="top">0xCE, 0xB8<br></td></tr><tr><td valign="top">€ (euro)<br></td><td valign="top">U+20AC<br></td><td valign="top">0xE2, 0x82, 0xAC<br></td></tr></tbody>
260 </table>
261 <br>
262 Some immediate observations:<br>
263 <ul>
264   <li>Null characters don't occur, so there is no problem to put such characters
265 in a null-terminated C string (without having to use the <code>wchar_t</code> type), although it is a little more difficult to get the length of the string.<br>
266     <br>
267   </li>
268   <li>Strict ASCII characters are encoded into 1 byte, which makes UTF-8
269 completely backward compatible with ASCII.  It doesn't change the size
270 of normal ASCII text strings or files.<br>
271     <br>
272   </li>
273   <li>Some characters need 3 bytes in UTF-8.  Indeed, all basic plane
274 characters (U+0000 to U+FFFF) can be encoded in 1, 2 or 3 bytes.</li>
275 </ul>
276 An excellent explanation of how to characters are encoded in UTF-8 can be found <a href="http://www.cl.cam.ac.uk/%7Emgk25/unicode.html#utf-8">on this page</a>.<br>
277 <br>
278 Some additional terminology regarding encoding schemes (less important here):<br>
279 <ul>
280   <li><b>UCS-2</b>: the encoding of Unicode characters in 2 bytes (16 bits) per character, can only encode U+0000 to U+FFFF (the basic plane)<br>
281     <br>
282   </li>
283   <li><b>UCS-4</b>: the encoding of Unicode characters in 4 bytes (32 bits) per character, can encode the entire UCS range<br>
284     <br>
285   </li>
286   <li><b>UTF-8</b>: the encoding scheme described above (UTF = UCS Transformation Format)<br>
287     <br>
288   </li>
289   <li><b>UTF-16</b>: an extension to UCS-2 to be able to encode characters
290 outside the basic plane (the 21-bit range of Unicode 3.1), by using a sequence
291 of two 16-bit characters<br>
292     <br>
293   </li>
294   <li><b>UTF-32</b>: the 4-byte encoding of the 21-bit range of Unicode 3.1, in fact the same as UCS-4<br>
295     <br>
296   </li>
297   <li><b>UTF-7</b>: can be safely forgotten... (a kind of 7-bit clean version of UTF-8)</li>
298 </ul>
299 Note that the byte order of UCS-2, UCS-4, UTF-16 and UTF-32 is not defined, so it can be big endian or little endian !<br>
300
301
302 <hr width="100%" size="2">
303 <pre><font size="-1">$Id$<br>$Name$</font><br></pre>
304
305 <br>
306
307 </body></html>