c语言判断是否是utf8字符串,计算字符个数
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /****************************************************************************
Unicode符号范围 | UTF-8编码方式
(十六进制) | (二进制)
0000 0000-0000 007F:0xxxxxxx
0000 0080-0000 07FF:110xxxxx 10xxxxxx
0000 0800-0000 FFFF:1110xxxx 10xxxxxx 10xxxxxx
0001 0000-001F FFFF:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
0020 0000-03FF FFFF:111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
0400 0000-7FFF FFFF:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
**************************************************************************/ unsigned char utf8_look_for_table[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1}; #define UTFLEN(x) utf8_look_for_table[(x)] //根据首字节,获取utf8字符所占字节数
inline int GetUtf8charByteNum(unsigned char ch)
{
int byteNum = 0; if (ch >= 0xFC && ch < 0xFE)
byteNum = 6;
else if (ch >= 0xF8)
byteNum = 5;
else if (ch >= 0xF0)
byteNum = 4;
else if (ch >= 0xE0)
byteNum = 3;
else if (ch >= 0xC0)
byteNum = 2;
else if (0 == (ch & 0x80))
byteNum = 1; return byteNum;
} //判断字符串是否是utf8格式
int IsUtf8Format(const char *str)
{
int byteNum = 0;
unsigned char ch;
const char *ptr = str; if (NULL == str)
return 0; while (*ptr != '\0')
{
ch = (unsigned char)*ptr;
if (byteNum == 0) //根据首字节特性判断该字符的字节数
{
if (0 == (byteNum = GetUtf8charByteNum(ch)))
return 0;
}
else //多字节字符,非首字节格式:10xxxxxx
{
if ((ch & 0xC0) != 0x80)
return 0;
}
byteNum--;
ptr++;
} if (byteNum > 0)
return 0; return 1;
} //计算utf8字符串字符个数
int GetUtf8Length(char *str)
{
int clen = 0;
int len = 0;
int byteNum = 0;
unsigned char ch;
char *ptr = str; if (NULL == str)
return 0; clen = strlen(str);
while (*ptr != '\0' && len < clen)
{
ch = (unsigned char)*ptr;
if (0 == (byteNum = GetUtf8charByteNum(ch)))
return 0;
ptr += byteNum;
len++;
} return len;
} int GetChargeNum(int len)
{
int num = 0; if (len > 70 && len <= 500)
{
if (!len % 67)
num = len / 67;
else
num = len / 67 + 1;
}
else if (len > 0)
num = 1; return num;
} int main(int argc, char **argv)
{
//char *str = "hello 你好呀!";
char *str;
int len = 0;
int num = 0; if (argc < 2)
return 0; str = argv[1];
printf("%s\n", str); if (!IsUtf8Format(str))
{
printf("the text is not the Format of utf8\n");
return 0;
} if (!(len = GetUtf8Length(str)))
return 0;
printf("the length of text: %d\n", len); if (!(num = GetChargeNum(len)))
return 0;
printf("the chargeNumber of sms: %d\n", num); return 1;
}
参考:
http://blog.sina.com.cn/s/blog_62b2318d0101d7kb.html
http://www.cnblogs.com/jiu0821/p/6371544.html
c语言判断是否是utf8字符串,计算字符个数的更多相关文章
- UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理
一.字符编码简介 1. ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(by ...
- [C++]_[获取Utf8字符串的字符个数和子字符串]
场景: 1.有时候须要统计utf8字符串的个数,单纯统计字节个数是不行的. 2.有时候也须要获取从某个位置開始的n个连续字符用于显示或计算. static int GetUtf8LetterNumbe ...
- length()返回当前字符串的字符个数
package seday01;/** * int length() * 返回当前字符串的字符个数 * @author xingsir * */public class LengthDemo { pu ...
- python统计字符串中字符个数
str = "xxx" result = {} for i in set(str):#set将字符串转为集合对象,用于去重,减少计算量 result[i] = str.count( ...
- python常见面试题讲解(二)计算字符个数
题目描述 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数.不区分大小写. 输入描述: 第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字 ...
- Python3基础 len函数 获得一个字符串的字符个数
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- C语言初学 使用while语句统计输入字符个数
#include<stdio.h> main() { int n=0; printf("输入任意个数的字符:\n"); while(getchar()!='\n')n+ ...
- 华为oj 计算字符个数
练手而已 #include <stdio.h> #include <string.h> int main(void) { char string[200]={'\0'}; in ...
- oracle统计字符串包含字符个数
函数:REGEXP_COUNT(); select REGEXP_COUNT('1,2,6,8,7,9',',') from dual 结果:5
随机推荐
- centos7 showdoc 手动安装部署
1.从 https://github.com/star7th/showdoc 下载压缩包: 2.解压文件,然后将showdoc-master下面的文件上传至服务器对应的文件下面: 3.在浏览器中输入 ...
- What makes an inferred latch? how To avoid creating inferred latches? when do you know you need latches?
What makes an inferred latch?For combinatorial logic, the output of the circuit is a function of inp ...
- (转)C#特性详解
本文转载自:http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...
- 怎么让eclipse调试的时候不进入 class文件中去
Eclipse -> Window ->Preferences ->Java ->Debug "Suspend execution on uncaught excep ...
- HTML 和 CSS
HTML html是英文hyper text mark-up language(超文本标记语言)的缩写,它是一种制作万维网页面标准语言. 内容摘要 Doctype 告诉浏览器使用什么样的htm ...
- L2-020. 功夫传人(dfs+vector 或者 邻接矩阵+dij+优先队列)
L2-020. 功夫传人 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一门武功能否传承久远并被发扬光大,是要看缘分的.一般来 ...
- Oracle 常见进程
1 服务器进程 专用服务器连接:数据连接跟服务器上的一个进程之间存在1:1的映射 共享服务器连接:多个会话共享一个服务器进程池,由一个调度程序分配 1.1 专用服务器连接 专用服务器连接模式下,客户连 ...
- centos7的xfs配置
XFS是扩展性高.高性能的文件系统.也是rhel7/centos7的默认文件系统.XFS支持metadata journaling,这使其能从crash中更快速的恢复.它也支持在挂载和活动的状态下进行 ...
- iOS多线程各种安全锁介绍 - 线程同步
一.atomic介绍 github对应Demo:https://github.com/Master-fd/LockDemo 在iOS中,@property 新增属性时,可以增加atomic选项,ato ...
- JS中,关于数组的练习题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...