Created
July 24, 2011 08:40
-
-
Save hpcx82/1102410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UTF-8 is a variable-width encoding, with each character represented by one to four bytes, and for each UTF character, all // its non-first byte are start with 10xxxxxx, so the number of UTF characters could be counted by count the bytes that not | |
// start with 10xxxxxx, and here comes the algorithrm | |
// http://en.wikipedia.org/wiki/UTF-8#Design | |
int strlen_utf8_c(char *s) { | |
int i = 0, j = 0; | |
while (s[i]) { | |
if ((s[i] & 0xc0) != 0x80) j++; | |
i++; | |
} | |
return j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment