##language:zh
#pragma section-numbers off
##含有章节索引导航的 ZPyUG 文章通用模板
<<TableOfContents>>
## 默许导航,请保留
<<Include(ZPyUGnav)>>


= UTF-8字符串长度的程序 =


##startInc
   
{{{ 
张沈鹏 <zsp007@gmail.com>
reply-to	python-cn@googlegroups.com
to	python-cn@googlegroups.com
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
}}}
顿悟。
"""

##endInc

----
'''反馈'''

创建 by -- ZoomQuiet [<<DateTime(2008-06-05T01:20:23Z)>>]