これはすべて UTF8 エンコーディングの一部です (これは Unicode の唯一のエンコーディング スキームです)。
サイズは、次のように最初のバイトを調べることで把握できます。
- ビットパターンで始まる場合、それ
"10" (0x80-0xbf)
はシーケンスの最初のバイトではないため、「0」または「11」で始まるバイトが見つかるまでバックアップする必要があります(コメントで指摘してくれた Jeffrey Hantin に感謝します) )。
- bit pattern で始まる場合は
"0" (0x00-0x7f)
1 バイトです。
- bit pattern で始まる場合は
"110" (0xc0-0xdf)
2 バイトです。
- bit pattern で始まる場合は
"1110" (0xe0-0xef)
3 バイトです。
- bit pattern で始まる場合は
"11110" (0xf0-0xf7)
4 バイトです。
これを示す表を複製しますが、オリジナルはウィキペディアの UTF8 ページ (こちら) にあります。
+----------------+----------+----------+----------+----------+
| Unicode | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
+----------------+----------+----------+----------+----------+
| U+0000-007F | 0xxxxxxx | | | |
| U+0080-07FF | 110yyyxx | 10xxxxxx | | |
| U+0800-FFFF | 1110yyyy | 10yyyyxx | 10xxxxxx | |
| U+10000-10FFFF | 11110zzz | 10zzyyyy | 10yyyyxx | 10xxxxxx |
+----------------+----------+----------+----------+----------+
上記の表の Unicode 文字は、次のビットから構成されます。
000z-zzzz yyyy-yyyy xxxx-xxxx
z
およびビットがy
指定されていない場合はゼロと見なされます。一部のバイトは、次のいずれかであるため、開始バイトとして不正と見なされます。
- 役に立たない: 0xc0 または 0xc1 で始まる 2 バイトのシーケンスは、実際には、1 バイトのシーケンスでより適切に表現できる 0x80 未満のコード ポイントを与えます。
- U+10FFFF を超える 4 バイト シーケンス、または 5 バイトおよび 6 バイト シーケンスに RFC3629 で使用されます。これらはバイト 0xf5 から 0xfd です。
- 未使用: バイト 0xfe と 0xff。
さらに、ビット「10」で始まらないマルチバイト シーケンス内の後続のバイトも不正です。
例として、シーケンス [0xf4,0x8a,0xaf,0x8d] を考えてみましょう。最初のバイトが 0xf0 と 0xf7 の間にあるため、これは 4 バイトのシーケンスです。
0xf4 0x8a 0xaf 0x8d
= 11110100 10001010 10101111 10001101
zzz zzyyyy yyyyxx xxxxxx
= 1 0000 1010 1011 1100 1101
z zzzz yyyy yyyy xxxx xxxx
= U+10ABCD
最初のバイトが 0xe6 (長さ = 3) の特定のクエリの場合、バイト シーケンスは次のようになります。
0xe6 0xbe 0xb3
= 11100110 10111110 10110011
yyyy yyyyxx xxxxxx
= 01101111 10110011
yyyyyyyy xxxxxxxx
= U+6FB3
ここでそのコードを見ると、それがあなたの質問にあったものであることがわかります: 澳.
デコードがどのように機能するかを示すために、アーカイブに戻って UTF8 処理コードを見つけました。完全なプログラムにするために少し変形する必要があり、エンコーディングは削除されました (質問は実際にはデコードに関するものだったため)。切り取りと貼り付けでエラーが発生していないことを願っています。
#include <stdio.h>
#include <string.h>
#define UTF8ERR_TOOSHORT -1
#define UTF8ERR_BADSTART -2
#define UTF8ERR_BADSUBSQ -3
typedef unsigned char uchar;
static int getUtf8 (uchar *pBytes, int *pLen) {
if (*pLen < 1) return UTF8ERR_TOOSHORT;
/* 1-byte sequence */
if (pBytes[0] <= 0x7f) {
*pLen = 1;
return pBytes[0];
}
/* Subsequent byte marker */
if (pBytes[0] <= 0xbf) return UTF8ERR_BADSTART;
/* 2-byte sequence */
if ((pBytes[0] == 0xc0) || (pBytes[0] == 0xc1)) return UTF8ERR_BADSTART;
if (pBytes[0] <= 0xdf) {
if (*pLen < 2) return UTF8ERR_TOOSHORT;
if ((pBytes[1] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
*pLen = 2;
return ((int)(pBytes[0] & 0x1f) << 6)
| (pBytes[1] & 0x3f);
}
/* 3-byte sequence */
if (pBytes[0] <= 0xef) {
if (*pLen < 3) return UTF8ERR_TOOSHORT;
if ((pBytes[1] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
if ((pBytes[2] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
*pLen = 3;
return ((int)(pBytes[0] & 0x0f) << 12)
| ((int)(pBytes[1] & 0x3f) << 6)
| (pBytes[2] & 0x3f);
}
/* 4-byte sequence */
if (pBytes[0] <= 0xf4) {
if (*pLen < 4) return UTF8ERR_TOOSHORT;
if ((pBytes[1] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
if ((pBytes[2] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
if ((pBytes[3] & 0xc0) != 0x80) return UTF8ERR_BADSUBSQ;
*pLen = 4;
return ((int)(pBytes[0] & 0x0f) << 18)
| ((int)(pBytes[1] & 0x3f) << 12)
| ((int)(pBytes[2] & 0x3f) << 6)
| (pBytes[3] & 0x3f);
}
return UTF8ERR_BADSTART;
}
static uchar htoc (char *h) {
uchar u = 0;
while (*h != '\0') {
if ((*h >= '0') && (*h <= '9'))
u = ((u & 0x0f) << 4) + *h - '0';
else
if ((*h >= 'a') && (*h <= 'f'))
u = ((u & 0x0f) << 4) + *h + 10 - 'a';
else
return 0;
h++;
}
return u;
}
int main (int argCount, char *argVar[]) {
int i;
uchar utf8[4];
int len = argCount - 1;
if (len != 4) {
printf ("Usage: utf8 <hex1> <hex2> <hex3> <hex4>\n");
return 1;
}
printf ("Input: (%d) %s %s %s %s\n",
len, argVar[1], argVar[2], argVar[3], argVar[4]);
for (i = 0; i < 4; i++)
utf8[i] = htoc (argVar[i+1]);
printf (" Becomes: (%d) %02x %02x %02x %02x\n",
len, utf8[0], utf8[1], utf8[2], utf8[3]);
if ((i = getUtf8 (&(utf8[0]), &len)) < 0)
printf ("Error %d\n", i);
else
printf (" Finally: U+%x, with length of %d\n", i, len);
return 0;
}
次のように、一連のバイトで実行できます(4が必要なので、0を使用してパディングします):
> utf8 f4 8a af 8d
Input: (4) f4 8a af 8d
Becomes: (4) f4 8a af 8d
Finally: U+10abcd, with length of 4
> utf8 e6 be b3 0
Input: (4) e6 be b3 0
Becomes: (4) e6 be b3 00
Finally: U+6fb3, with length of 3
> utf8 41 0 0 0
Input: (4) 41 0 0 0
Becomes: (4) 41 00 00 00
Finally: U+41, with length of 1
> utf8 87 0 0 0
Input: (4) 87 0 0 0
Becomes: (4) 87 00 00 00
Error -2
> utf8 f4 8a af ff
Input: (4) f4 8a af ff
Becomes: (4) f4 8a af ff
Error -3
> utf8 c4 80 0 0
Input: (4) c4 80 0 0
Becomes: (4) c4 80 00 00
Finally: U+100, with length of 2