UTF-8字符串长度的程序

张沈鹏 <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Thu, Jun 5, 2008 at 9:12 AM
subject [CPyUG:53685] 谁能解释一下我在这个网页看到的UTF-8字符串长度的程序?

谁能解释一下我在这个网页看到的UTF-8字符串长度的程序?

http://wangcong.org/blog/?p=357

在这里看到这么一个计算UTF-8字符串长度的程序:

  1.     int my_strlen_utf8_c(char *s) {
  2.       int i = 0, j = 0;
  3.       while (s[i]) {
  4.         if ((s[i] & 0xc0) != 0x80) j++;
  5.         i++;
  6.       }
  7.       return j;
  8.     }

不解。查wikipedia,得一表:

00000000 00000000 0zzzzzzz 0zzzzzzz
00000000 00000yyy yyzzzzzz 110yyyyy 10zzzzzz
00000000 xxxxyyyy yyzzzzzz 1110xxxx 10yyyyyy 10zzzzzz
000wwwxx xxxxyyyy yyzzzzzz 11110www 10xxxxxx 10yyyyyy 10zzzzzz

顿悟。 """


反馈

创建 by -- ZoomQuiet [2008-06-05 01:20:23]

MiscItems/2008-06-05 (last edited 2009-12-25 07:09:26 by localhost)