Bei dem Zeichensatz "UTF-8" handelt es sich nicht um einen Zeichensatz, wie z.B. "Windows Codepage 1252", sondern vielmehr um eine Codierung von Unicode-Zeichen. Hierbei werden die wichigeren Zeichen (z.B. 0..9, A..Z, a..z) in einem Byte codiert, etwas weniger wichtigere Zeichen (z.B. ÄÖÜäöüß) in zwei Byte. Die Abstufung geht bis zu einer Codierung von 4 Byte.
Der von KEYTAB implementierte Algorithmus zur Umwandlung von Unicode in UTF-8 lautet:
Unicode 'unicode' zwischen
$00..$7F: ein Byte
'unicode'
$80..$7FF: zwei Byte
%11xx xxxx - Bit 11 bis Bit 6 von 'unicode'
%10xx xxxx - Bit 5 bis Bit 0 von 'unicode'
| $00 | $7F | 1 Byte | |
| %0xxx xxxx | Bit 6 bis Bit 0 | ||
| $80 | $7FF | 2 Byte | |
| %110x xxxx | Bit 10 bis Bit 6 | ||
| %10xx xxxx | Bit 5 bis Bit 0 | ||
| $800 | $FFFF | 3 Byte | |
| %1110 xxxx | Bit 15 bis Bit 12 | ||
| %10xx xxxx | Bit 11 bis Bit 6 | ||
| %10xx xxxx | Bit 5 bis Bit 0 | ||
| $10000 | $200000 | 4 Byte |
bzw. im C-Source
/* Zeichen unter 0x80 sind nicht codiert */
if( lunicode < 0x80 )
{
string[0] = (char)(lunicode & 0x7Fl);
used = 1;
}
else if( lunicode < 0x800 )
{
string[0] = 0xC0 | (char)((lunicode>>6) & 0x3F);
string[1] = 0x80 | (char)(lunicode & 0x3F);
used = 2;
}
else if( lunicode < 0x10000l )
{
string[0] = 0xE0 | (char)((lunicode>>12) & 0x3F);
string[1] = 0x80 | (char)((lunicode>>6) & 0x3F);
string[2] = 0x80 | (char)(lunicode & 0x3F);
used = 3;
}
else if( lunicode < 0x200000l )
{
string[0] = 0xF0 | (char)((lunicode>>18) & 0x3F);
string[1] = 0x80 | (char)((lunicode>>12) & 0x3F);
string[2] = 0x80 | (char)((lunicode>>6) & 0x3F);
string[3] = 0x80 | (char)(lunicode & 0x3F);
used = 4;
}