使用C语言计算utf-8字符串长度
来源:互联网 发布:阿里云合作伙伴等级 编辑:程序博客网 时间:2024/06/05 15:33
#include
#include
#include
//utf8字符长度1-6,可以根据每个字符第一个字节判断整个字符长度
//0xxxxxxx
//110xxxxx10xxxxxx
//1110xxxx 10xxxxxx10xxxxxx
//11110xxx 10xxxxxx10xxxxxx 10xxxxxx
//111110xx 10xxxxxx10xxxxxx 10xxxxxx 10xxxxxx
//1111110x 10xxxxxx10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//
//定义查找表,长度256,表中的数值表示以此为起始字节的utf8字符长度
unsigned
{
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
3,
4,
};
#define UTFLEN(x)
//计算str字符数目
int
{
int
int
for(char
*ptr!=0&&len<clen;
len++, ptr+=UTFLEN((unsigned
return
}
//get子串
char*subUtfString(char
{
int
if(start>= len)
if(end> len) end = len;
char
for(int
char
for(int
int
char
memcpy(retStr, sptr,retLen);
retStr[retLen] =
return
}
int
{
char
printf("%s\n", str);
for(char
unsigned
printf("str[%d] is a word character with %dbytes\n",c,
ptr +=
}
printf("%d\n",
char
if(sub) {
printf("%s\n", sub);
free(sub);
sub =
}
return
}
程序在osx下调试通过。
在vs2005下,默认的文件编码格式是gb2312,需要将文件保存成utf8格式。
原文地址:http://blog.sina.com.cn/s/blog_62b2318d0101d7kb.html
- 使用C语言计算utf-8字符串长度
- 使用C语言计算utf-8字符串长度
- 使用C语言计算utf-8字符串长度
- 使用C语言计算utf-8字符串长度
- 使用C语言计算utf-8字符串长度 和取子字符串-转
- 对utf-8字符串计算长度
- JS中计算字符串UTF-8长度
- C语言计算字符串的长度strlen
- C语言基础 计算字符串的长度
- 计算 UTF-8 字符串长度(忽略字节的方案)
- 【C语言】02-C语言字符串长度和OC字符串对象长度计算的比较
- C语言 - 字符串长度函数使用
- C语言 字符串拷贝和长度计算(不用库函数)
- 【c语言】编写一个函数计算字符串长度
- 用strlen函数计算字符串的长度(C语言)
- strlen 是C语言的一个库函数计算字符串长度
- C语言 字符串常用函数(计算长度,拷贝,拼接,比较)
- C语言计算字符串长度的几种方法
- 原型模式(Prototype Pattern)
- [java]截取字符串
- Cisco网络设备配置入门必读的几个命令
- Ajax学习笔记
- python的初级训练
- 使用C语言计算utf-8字符串长度
- [ASP.NET MVC 小牛之路]18 - Web API
- 程序员面试宝典一周目第三天
- 知识点二:contentProvider的使用
- delphi使用细节
- iptables限制MACIP绑定上网+Squid 透明代理How to
- 图像分割(Image Segmentation)
- eclipse或IDEA连接魅蓝
- 注解-处理request