统计UTF-8编码方式字符串中的符号个数
生活随笔
收集整理的這篇文章主要介紹了
统计UTF-8编码方式字符串中的符号个数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法1:轉載自:http://blog.csdn.net/chrisniu1984/article/details/7359908
int utf8_wchar_count(char *buffer, int len)
{#define UTF8_ASCII(byte) ( ((unsigned char)(byte)>=0x00)&&((unsigned char)(byte)<=0x7F) ) #define UTF8_FIRST(byte) ( ((unsigned char)(byte)>=0xC0)&&((unsigned char)(byte)<=0xFD) ) #define UTF8_OTHER(byte) ( ((unsigned char)(byte)>=0x80)&&((unsigned char)(byte)<=0xBF) ) char *p = 0; long count = 0; if (!buffer || len <= 0) { return 0; } for(p=buffer; p<buffer+len; p++) { if (UTF8_ASCII(*p) || (UTF8_FIRST(*p))) { count++; } } return count;
}
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
方法2:轉載自:http://www.tuicool.com/articles/v67jay
int get_utf8_count(const std::string &input) {int length = 0;for (size_t i = 0, len = 0; i != input.length(); i += len) {unsigned char byte = input[i];if (byte >= 0xFC) len = 6; else if (byte >= 0xF8)len = 5;else if (byte >= 0xF0)len = 4;else if (byte >= 0xE0)len = 3;else if (byte >= 0xC0)len = 2;elselen = 1;length ++;} return length; }超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的统计UTF-8编码方式字符串中的符号个数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合成复用原则
- 下一篇: utf-8编码的字符串转成unicode