Pet*_*ras 20 unicode encoding cjk
我已经阅读了Joel的文章"绝对最低限度,每个软件开发人员绝对必须知道关于Unicode和字符集(没有借口!)",但仍然不了解所有细节.一个例子将说明我的问题.请看下面这个文件:
替代文字http://www.yart.com.au/stackoverflow/unicode2.png
我在二进制编辑器中打开文件,仔细检查第一个汉字旁边的三个a中的最后一个:
替代文字http://www.yart.com.au/stackoverflow/unicode1.png
乔尔说:
在UTF-8中,0-127的每个代码点都存储在一个字节中.仅使用2,3存储代码点128及以上,实际上最多6个字节.
编辑也说:
如果是这样,是什么表明解释超过2个字节?这是如何用E6后面的字节表示的?
我的汉字是以2,3,4,5或6字节存储的吗?
Jon*_*ler 28
如果编码是UTF-8,则下表显示如何将Unicode代码点(最多21位)转换为UTF-8编码:
Scalar Value 1st Byte 2nd Byte 3rd Byte 4th Byte
00000000 0xxxxxxx 0xxxxxxx
00000yyy yyxxxxxx 110yyyyy 10xxxxxx
zzzzyyyy yyxxxxxx 1110zzzz 10yyyyyy 10xxxxxx
000uuuuu zzzzyyyy yyxxxxxx 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
Run Code Online (Sandbox Code Playgroud)
有许多非允许值 - 特别是字节0xC1,0xC2和0xF5 - 0xFF永远不会出现在格式良好的UTF-8中.还有许多其他的verboten组合.不规则性在第1字节和第2字节列中.请注意,代码U + D800 - U + DFFF是为UTF-16代理保留的,不能出现在有效的UTF-8中.
Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte
U+0000..U+007F 00..7F
U+0080..U+07FF C2..DF 80..BF
U+0800..U+0FFF E0 A0..BF 80..BF
U+1000..U+CFFF E1..EC 80..BF 80..BF
U+D000..U+D7FF ED 80..9F 80..BF
U+E000..U+FFFF EE..EF 80..BF 80..BF
U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
Run Code Online (Sandbox Code Playgroud)
这些表从Unicode标准版本5.1 中解除.
在问题中,偏移量为0x0010 .. 0x008F的材料产生:
0x61 = U+0061
0x61 = U+0061
0x61 = U+0061
0xE6 0xBE 0xB3 = U+6FB3
0xE5 0xA4 0xA7 = U+5927
0xE5 0x88 0xA9 = U+5229
0xE4 0xBA 0x9A = U+4E9A
0xE4 0xB8 0xAD = U+4E2D
0xE6 0x96 0x87 = U+6587
0xE8 0xAE 0xBA = U+8BBA
0xE5 0x9D 0x9B = U+575B
0x2C = U+002C
0xE6 0xBE 0xB3 = U+6FB3
0xE6 0xB4 0xB2 = U+6D32
0xE8 0xAE 0xBA = U+8BBA
0xE5 0x9D 0x9B = U+575B
0x2C = U+002C
0xE6 0xBE 0xB3 = U+6FB3
0xE6 0xB4 0xB2 = U+6D32
0xE6 0x96 0xB0 = U+65B0
0xE9 0x97 0xBB = U+95FB
0x2C = U+002C
0xE6 0xBE 0xB3 = U+6FB3
0xE6 0xB4 0xB2 = U+6D32
0xE4 0xB8 0xAD = U+4E2D
0xE6 0x96 0x87 = U+6587
0xE7 0xBD 0x91 = U+7F51
0xE7 0xAB 0x99 = U+7AD9
0x2C = U+002C
0xE6 0xBE 0xB3 = U+6FB3
0xE5 0xA4 0xA7 = U+5927
0xE5 0x88 0xA9 = U+5229
0xE4 0xBA 0x9A = U+4E9A
0xE6 0x9C 0x80 = U+6700
0xE5 0xA4 0xA7 = U+5927
0xE7 0x9A 0x84 = U+7684
0xE5 0x8D 0x8E = U+534E
0x2D = U+002D
0x29 = U+0029
0xE5 0xA5 0xA5 = U+5965
0xE5 0xB0 0xBA = U+5C3A
0xE7 0xBD 0x91 = U+7F51
0x26 = U+0026
0x6C = U+006C
0x74 = U+0074
0x3B = U+003B
Run Code Online (Sandbox Code Playgroud)
pax*_*blo 22
这是UTF8编码的所有部分(这只是Unicode的一种编码方案).
通过检查第一个字节可以计算出大小,如下所示:
"10" (0x80-0xbf)
,它不是序列的第一个字节,你应该备份直到你找到开始,任何以"0"或"11"开头的字节(感谢Jeffrey Hantin在评论中指出这一点) )."0" (0x00-0x7f)
1个字节."110" (0xc0-0xdf)
2个字节."1110" (0xe0-0xef)
3个字节."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 |
+----------------+----------+----------+----------+----------+
Run Code Online (Sandbox Code Playgroud)
上表中的Unicode字符由位构成:
000z-zzzz yyyy-yyyy xxxx-xxxx
Run Code Online (Sandbox Code Playgroud)
其中z
和y
位被假定为零,而没有给出它们.一些字节被认为是非法的起始字节,因为它们是:
此外,多字节序列中不以位"10"开头的后续字节也是非法的.
例如,考虑序列[0xf4,0x8a,0xaf,0x8d].这是一个4字节序列,因为第一个字节落在0xf0和0xf7之间.
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
Run Code Online (Sandbox Code Playgroud)
对于第一个字节为0xe6(长度= 3)的特定查询,字节序列为:
0xe6 0xbe 0xb3
= 11100110 10111110 10110011
yyyy yyyyxx xxxxxx
= 01101111 10110011
yyyyyyyy xxxxxxxx
= U+6FB3
Run Code Online (Sandbox Code Playgroud)
如果你在这里查看代码,你会发现它是你问题中的代码:澳.
为了说明解码是如何工作的,我回到我的档案中找到了我的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;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用您的字节序列运行它(您需要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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18622 次 |
最近记录: |