#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字符串,计算字符个数的更多相关文章

  1. UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理

    一.字符编码简介 1. ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(by ...

  2. [C++]_[获取Utf8字符串的字符个数和子字符串]

    场景: 1.有时候须要统计utf8字符串的个数,单纯统计字节个数是不行的. 2.有时候也须要获取从某个位置開始的n个连续字符用于显示或计算. static int GetUtf8LetterNumbe ...

  3. length()返回当前字符串的字符个数

    package seday01;/** * int length() * 返回当前字符串的字符个数 * @author xingsir * */public class LengthDemo { pu ...

  4. python统计字符串中字符个数

    str = "xxx" result = {} for i in set(str):#set将字符串转为集合对象,用于去重,减少计算量 result[i] = str.count( ...

  5. python常见面试题讲解(二)计算字符个数

    题目描述 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数.不区分大小写. 输入描述: 第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字 ...

  6. Python3基础 len函数 获得一个字符串的字符个数

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  7. C语言初学 使用while语句统计输入字符个数

    #include<stdio.h> main() { int n=0; printf("输入任意个数的字符:\n"); while(getchar()!='\n')n+ ...

  8. 华为oj 计算字符个数

    练手而已 #include <stdio.h> #include <string.h> int main(void) { char string[200]={'\0'}; in ...

  9. oracle统计字符串包含字符个数

    函数:REGEXP_COUNT(); select REGEXP_COUNT('1,2,6,8,7,9',',') from dual 结果:5

随机推荐

  1. springmvc 加载静态文件失败

    header.jsp,部分代码 <head> <title>QA|VIS_PLATFORM</title> <meta content="width ...

  2. 使用Json Template在Azure China创建ARM类型的虚拟机

    前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM.并且介绍了如何用Azure CLI和PowerShell创建虚拟机.本文将介绍如何采用Json的Template来创建基于ARM的VM ...

  3. [转载]rmmod: can't change directory to '/lib/modules': No such file or directory

    转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...

  4. 怎么让eclipse调试的时候不进入 class文件中去

    Eclipse -> Window ->Preferences ->Java ->Debug "Suspend execution on uncaught excep ...

  5. 机器学习:评价分类结果(ROC 曲线)

    一.基础理解 1)定义 ROC(Receiver Operation Characteristic Curve) 定义:描述 TPR 和 FPR 之间的关系: 功能:应用于比较两个模型的优劣: 模型不 ...

  6. Mycat-server-1.6.5 常见分片方式

    Mycat-server-1.6.5 常见分片方式 1 安装 [root@hongquan1 soft]# tar zxvf Mycat-server-1.6.5-release-2018012222 ...

  7. [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?

    3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...

  8. [更新中]【South使用总结】django开发中使用South进行数据库迁移

    Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...

  9. UML在需求分析阶段的应用

    转自:https://www.cnblogs.com/fuhaots2009/p/3430666.html 上一篇博客写了uml在软件开发过程中的应用,这以篇要详细介绍一下UML在需求分析过程中的应用 ...

  10. C# WinForm中如何让当前应用程序只允许启动一个实例

    我们在WinForm开发中,很多情况下是需要只允许让用户运行一个实例,那么代码其实很简单.只需要修改Program.cs文件,代码如下 static class Program { /// <s ...