<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN">

<head>
<meta charset="utf-8" />
<title>UNICODE</title>
<style type="text/css">
  body {
    counter-reset: item2;
    font-size: 12pt;
  }
  h2:before {
    content: counter(item2) " "; 
    counter-increment: item2 1;
  }
  h2 {
    counter-reset: item3;
  }
  h3:before {
    content: counter(item2) "." counter(item3) " "; 
    counter-increment: item3;
  }
  h3 {
    counter-reset: item4;
  }
  h4:before {
    content: counter(item2) "." counter(item3) "." counter(item4) " "; 
    counter-increment: item4;
  }
  div.indented {
    margin-left: 40px;
  }
  div.cite 
  { 
    border: solid 2px;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
</style>
</head>
<body>

<center><h1>UNICODE</h1></center>

<h2>Introduction</h2>

<p>
The dictionaries of Agheyisi, Melzian and Thomas as well as the corpus of Thomas
contain many special characters. To display these characters the UNICODE UTF-8 
standard is used.
</p>

<h2>Hexadecimal notation</h2>

<p>
An <i>hexadecimal digit</i> is one of the characters '0',...,'9','A',...,'F'.
Each hexadecimal digit denotes a number:
</p>
<div class='cite'>
<code>
'0'=0 '1'=1 '2'=2 '3'=3 '4'=4 '5'=5 '6'=6 '7'=7 '8'=8 '9'=9
</code><br></br><br></br>
<code>
'A'=10 'B'=11 'C'=12 'D'=13 'E'=14 'F'=15
</code>
</div>

<p>
A <i>hexadecimal number</i> is a number written in <i>hexadecimal notation</i>, that is,
with base 16. For example:
</p>
<div class='cite'>
<code>
203F = 2*16*16*16 + 0*16*16 + 3*16 + 14 = 8254
</code>
</div>

<h2>UNICODE</h2>

<p>
<a href="https://home.unicode.org/">UNICODE</a>
is a standard that assigns a <i>code number</i> to a character
(<a href="https://en.wikipedia.org/wiki/Unicode">wikipedia</a>).
Most Unicode characters have a code number between 0 en 65,535 and thus need four hexadecimal digits
in hexadecimal notation.
</p>

<p>
Codenumbers between 0 and 65,535 are usualy written as <b>U+nnnn</b> where n is a hexadecimal digit.
For example, that character '‿' (UNDERTIE) has codenumber U+203F 
(8254 decimal).
</p>

<p>
An <i>encoding</i> is a mapping from code numbers (which represent characters) to sequences
of code units. A code unit is in practice an octet (8-bit byte), a double octet (16-bit quantity),
or a quadruple octet (32-bit quantity).
</p>

<p>
The encoding scheme used here is UTF-8.
</p>

<h2>UTF-8</h2>

<h3>UTF-8 encoding</h3>

<p>
Korpela, Jukka K - Unicode explained, O'Reilly (2006):
</p>

<div class="cite">
<p>
UTF-8 uses 8-bit code units, and it represents characters in the Basic Latin (ASCII)
range U+0000 to U+007F efficiently, one code unit per character. On the other hand,
this implies that all other characters use at least two code units, which all have the most
significant bit set—i.e., they are in the range 80 to FF (hexadecimal). More exactly,
they are in the range 80 to 9F. This means that when there is a code unit in the range
00 to 7F in UTF-8 data, we can know that it represents a Basic Latin character and
cannot be part of the representation of some other character.
</p>
<p>
These structural decisions imply that UTF-8 is relatively inefficient, since it leaves many
simple combinations unused. There is yet another principle that has a similar effect. In
a representation of any character other than Basic Latin characters, the first (leading)
code unit is from a specific range, and all the subsequent (trailing) code units are from
a different range.
</p>

<b>UTF-8 Encoding Algorithm</b>

<p>
For a character outside the Basic Latin block, UTF-8 uses two, three, or four octets.
You might encounter specifications that describe UTF-8 as using up to six octets per
character, but they reflect definitions that did not restrict the Unicode coding space the
way it has now been restricted.
</p>
<p>
The UTF-8 algorithm is described in Table 6-1. The first column specifies a bit pattern,
in 16 or 21 bits, grouped for readability. The other columns indicate how the pattern
is mapped to code units (octets), represented here as bit patterns.
</p>

<i>Table 6-1. UTF-8 encoding algorithm</i>
<table>
<tr>
<th>Code number in binary</th>
<th>Octet 1</th>
<th>Octet 2</th>
<th>Octet 3</th>
<th>Octet 4</th>
</tr>
<tr>
<td>00000000 0xxxxxxx</td>
<td>0xxxxxxx</td>
<td></td>
<td></td>
</tr>
<tr>
<td>00000yyy yyxxxxxx</td>
<td>110yyyyy</td>
<td>10xxxxxx</td>
</tr>
<tr>
<td>zzzzyyyy yyxxxxxx</td>
<td>1110zzzz</td>
<td>10yyyyyy</td>
<td>10xxxxxx</td>
</tr>
<tr>
<td>uuuww zzzzyyyy yyxxxxxx</td>
<td>11110uuu</td>
<td>10wwzzzz</td>
<td>10yyyyyy</td>
<td>10xxxxxx</td>
</tr>
</table>

<p>
Thus, the UTF-8 encoding uses bit combinations of very specific types in the octets. If
you pick up an octet from UTF-8 encoded data, you can immediately see its role. If the
first bit is 0, the octet is a single-octet representation of a (Basic Latin) character. Otherwise,
you look at the second bit as well. If it is 0, you know that you have a second,
third, or fourth octet of a multioctet representation of a character. Otherwise, you have
the first octet of such a representation, and the initial bits 110, 1110, or 1111 reveal
whether the representation is two, three, or four octets long.
</p>
<p>
Thus, interpreting (decoding) UTF-8 is straightforward, too. You take an octet, match
it with the patterns in column “Octet 1” in Table 6-1, and read zero to three additional
octets accordingly. Then you construct the binary representation of the code number
from the bit sequences you extract from the octets. Naturally, nobody wants to do this
by hand, but the point is that this can be implemented efficiently, as operations on bit
fields. A correct implementation of Unicode has to signal an error, if there is data that
does match any of the defined patterns.
</p>
<p>
A quick way to find out the UTF-8 encoding of a string is to visit http://www.goo
gle.com on any modern browser, type the string into the keyword box, and hit Search.
Then just look at the address field of the browser. For example, if you type pâté, the
address field will contain http://www.google.com/search?hl=en&amp;lr=&amp;q=p%C3%A2t
%C3%A9, so you can see that â is encoded as the octets C3 A2 and é as octets C3 A9.
(In some situations, this does not work since Google does not use UTF-8. In that case,
use the URL http://www.google.com/webhp?ie=UTF-8 to force the input encoding to
UTF‑8.)
</p>

<b>Some Properties of UTF-8</b>

<p>
Due to the algorithm, the octets appearing in UTF-8 are limited to certain ranges, as
shown in Table 6-2. In particular, octets C0 and C1 and F5 through FF do not appear
in UTF-8. Other octets may appear in specific contexts only. This means that if you
have a large file that is not, in fact, character data in UTF-8 and you try to read it as
UTF-8, it is most probable that errors will be signaled.
</p>

<i>Table 6-2. Octet ranges in UTF-8</i>
<table>
<tr>
<th>Code range</th>
<th>Octet 1</th>
<th>Octet 2</th>
<th>Octet 3</th>
<th>Octet 4</th>
</tr>
<tr>
<td>U+0000..U+007F</td>
<td>00..7F</td>
</tr>
<tr>
<td>U+0080..U+07FF</td>
<td>C2..DF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+0800..U+0FFF</td>
<td>E0</td>
<td>A0..BF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+1000..U+CFFF</td>
<td>E1..EC</td>
<td>80..BF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+D000..U+D7FF</td>
<td>ED</td>
<td>80..9F</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+E000..U+FFFF</td>
<td>EE..EF</td>
<td>80..BF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+10000..U+3FFFF</td>
<td>F0</td>
<td>90..BF</td>
<td>80..BF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+40000..U+FFFFF</td>
<td>F1..F3</td>
<td>80..BF</td>
<td>80..BF</td>
<td>80..BF</td>
</tr>
<tr>
<td>U+100000..U+10FFFF</td>
<td>F4</td>
<td>80..8F</td>
<td>80..BF</td>
<td>80..BF</td>
</tr>
</table>

<p>
Similarly to UTF-16, UTF-8 makes it impossible to access the nth character of a string
directly. UTF-8 is robust, though: if a code unit is corrupted, other characters will be
processed correctly. The reason is that UTF-8 has been designed so that a code unit
starting the representation of a character can be recognized as such, even if the preceding
code unit is in error.
</p>
</div>

<h3>UTF-8 encoding tables</h3>

<p>
A table of Unicode characters with the UTF-8 code is given at the website
<a href="http://www.utf8-chartable.de/">UTF-8 encoding table and Unicode characters</a>
</p>

<p>
The table is devided into blocks. Entries in a block are displayed as follows:
</p>
<div class="cite">
<pre>
    U+2015	―	e2 80 95	HORIZONTAL BAR
</pre>
</div>

<p>
The first column contains the Unicode code number.<br></br>
The second column contains the character.<br></br>
The third column contains the UTF-8 code (hex.)<br></br>
The fourth column contains the name of the character.
</p>

<h3>Combining characters</h3>

<p>
The Unicode characters between U+0300 and U+036F are diacritical marks that combine with the
preceding Unicode character. A normal character can be followed by several diacritical markers.
</p>

<p>
For example:
</p>

<div class="cite">
ọ́&nbsp;&nbsp;<code>U+006f U+0301 U+0323</code><br></br>
<br></br>
with<br></br>
<br></br>
<code>U+006F&nbsp;&nbsp;</code>o	<code>&nbsp;&nbsp;6f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LATIN SMALL LETTER O</code><br></br>
<code>U+0301&nbsp;&nbsp;</code>	́	<code>&nbsp;&nbsp;cc 81&nbsp;&nbsp;COMBINING ACUTE ACCENT</code><br></br>
<code>U+0323&nbsp;&nbsp;</code>	̣	<code>&nbsp;&nbsp;cc a3&nbsp;&nbsp;COMBINING DOT BELOW</code>
</div>

<p>
Not all display software will display such characters correctly. The diacritical markers may
appear after the leading character.
</p>

<h3>Linux commands</h3>

<h4>echo</h4>

<p>
The <b>echo</b> command prints its arguments. For example:
</p>
<div class="cite">
<code>localhost: $ echo hello</code><br></br>
<code>hello</code><br></br>
<code>localhost: $ </code>
</div>

<p>
The <b>-n</b> option to <b>echo</b> causes it <i>not</i> to print a LINEFEED after printing its
arguments. For example:
</p>
<div class="cite">
<code>localhost: $ echo -n hello</code><br></br>
<code>hellolocalhost: $ </code>
</div>

<p>
The <b>-e</b> option to <b>echo</b> turns on the interpretation of backslash-escaped characters.
For example '\n' represents a NEWLINE and '\t' a tab.
'\x' followed by two hexadecimal digits is the hexadecimal representation of one byte 
(a number between 0 and 255).
</p>
<p>
For example:
</p>
<div class="cite">
<code>localhost: $ echo "\x3F\x21"</code><br></br>
<code>\x3F\x21</code><br></br>
<code>localhost: $ echo -e "\x3F\x21"</code><br></br>
<code>?!</code><br></br>
<code>localhost: $ </code>
</div>

<p>
Here, '\x3F' is the hexadecimal representation of the character '?' (1 byte) and
'\x21' is the hexadecimal representation of the character '!'.
</p>

<h4>iconv</h4>

<p>
The <b>iconv</b> command converts encoding of given files from one encoding to another.
Instead of specifying a filename one can pipe the output of a command to the <b>iconv</b> command.
For example:
</p>
<div class="cite">
<code>localhost: $ echo -ne "\xE5\x02" | iconv -fUNICODE -tUTF-8</code><br></br>
˥
<code>localhost: $ </code>
</div>

<p>
where '˥' is the unicode character U+02E5 (note the inverted order of the bytes).
</p>

<p>
Here UNICODE probably means UTF-16 coding (16-bit code unit, that is, a one unsigned 16-bit integer).
The bash shell apparently uses UTF-8.
</p>

<h4>Some utility functions</h4>

<p>
With the <b>echo</b> and <b>iconv</b> commands we can build simple functions to display
the characters corresponding to a given Unicode code number.
</p>

<div class="cite">
<pre>
function unicode () 
  { echo -ne "\x${1:2:2}\x${1:0:2}" | iconv -f=UNICODE -t=UTF-8; 
    echo ""; 
  }

function unicode2 () 
  { echo -ne "\x${1:2:2}\x${1:0:2}\x${2:2:2}\x${2:0:2}" | iconv -f=UNICODE -t=UTF-8; 
    echo ""; 
  }

function unicode3 () 
  { echo -ne "\x${1:2:2}\x${1:0:2}\x${2:2:2}\x${2:0:2}\x${3:2:2}\x${3:0:2}" | iconv -f=UNICODE -t=UTF-8; 
    echo ""; 
  }
</pre>
</div>

<p>
These functions take respectively one, two and three code numbers as input (without the 'U+').
The expression '${3:2:2}' means the substring of the third argument starting at the third 
character (the first character has index 0) and with length 2.
This substring is then preceded with '\x' and concatenated to other similar expressions. The result
is then via the <b>echo</b> command input to the <b>iconv</b> command. 
</p>
<p>
For example:
</p>

<div class="cite">
<code>localhost: $ unicode 2019</code><br></br>
’ <br></br>
<code>localhost: $ unicode2 006F 0349</code><br></br>
o͉ <br></br>
<code>localhost: $ unicode3 006F 0349 0301</code><br></br>
ó͉ <br></br>
<code>localhost: $ </code>
</div>

<p>
The UTF-8 code of a character can be display with the 'hexdump -C' command.
</p>

<div class="cite">
<code>localhost: $ unicode 02E5</code><br></br>
˥<br></br>
<code>localhost: $ echo </code>˥<code> | hexdump -C</code><br></br>
<code>00000000  cb a5 0a                                          |...|</code><br></br>
<code>00000003</code><br></br>
<code>localhost: $ </code>
</div>

<p>
The UTF-8 code of '˥' (code number U+02E5) is 'cb a5'. '0a' is the LINEFEED added by the
<b>echo</b> command.
</p>

<h2>Ucodes used</h2>

<p>
TBD formatting characters in de masterfiles
</p>

<p>
<a href="Agheyisi-ucode-characters.html">Agheyisi dictionary characters</a>
</p>

<p>
<a href="Agheyisi-corpus-ucode-characters.html">Agheyisi corpus characters</a>
</p>

<p>
<a href="Melzian-ucode-characters.html">Melzian characters</a>
</p>

<p>
<a href="Thomas-dictionary-ucode-characters.html">Thomas dictionary characters</a>
</p>

<p>
<a href="Thomas-corpus-ucode-characters.html">Thomas corpus characters</a>
</p>

<p>
<a href="Transliteration.html">Thomas transliteration table</a>
</p>

<p>
<a href="ucode-codes.html">UNICODE codes</a>
</p>

<hr></hr>
<i>Last update: 14-10-2022</i>
</body>
</html>
