跨平台utf8与unicode的互转
来源:互联网 发布:如何查网址域名 编辑:程序博客网 时间:2024/06/07 10:56
这几天帮同事看一个聊天敏感词过滤问题,需求是在ndk环境下,把用户输入的句子中不健康的词语替换成*,开始时需要先将用户输入转成wchar_t*,用了mbstowcs,在本地linux环境下测试没有问题,但是比较坑的是ndk安卓真机环境下不行,后来干脆自己写了utf8与unicode相互转换的一组函数,utf8sToUnicodes是将utf8字符串转成unicode数组,unicodesToUtf8s是将unicode数组转成utf8字符串。
\\根据多字节字符串的第一个字符返回该unicode占的字节数int getUtf8Len(const char* utfs) { unsigned int in = utfs[0]; in = in & 0x000000ff; if (in >= 0x00 && in < 0xC0) return 1; else if (in >= 0xC0 && in < 0xE0) return 2; else if (in >= 0xE0 && in < 0xF0) return 3; else return 0;}\\将多个字符组成的一个字转换成unicodeint utf8ToUnicode(const char* utf, int len) { unsigned int rst; if (1 == len) rst = utf[0]; else if (2 == len) { unsigned int part1 = utf[0] & 0x1F; part1 << 6; unsigned int part2 = utf[1] & 0x3F; rst = part1 | part2; } else if (3 == len) { unsigned int part1 = utf[0] & 0x0F; part1 = part1 << 12; unsigned int part2 = utf[1] & 0x3F; part2 = part2 << 6; unsigned int part3 = utf[2] & 0x3F; rst = part1 | part2 | part3; } else { rst = 0; } return rst;}\\将Unicode转换成多字节字符void unicodeToUtf(const unsigned int unicode, char* utf) { if (unicode < 0x80) { utf[0] = unicode >> 0 & 0x7F | 0x00; utf[1] = '\0'; } else if (unicode < 0x0800) { utf[0] = unicode >> 6 & 0x1F | 0xC0; utf[1] = unicode >> 0 & 0x3F | 0x80; utf[2] = '\0'; } else if (unicode < 0x010000) { utf[0] = unicode >> 12 & 0x0F | 0xE0; utf[1] = unicode >> 6 & 0x3F | 0x80; utf[2] = unicode >> 0 & 0x3F | 0x80; utf[3] = '\0'; }}\\将多字节字符串转换成unicode数组int utf8sToUnicodes(const char* utfs, unsigned int * unicodes) { int len = strlen(utfs); int mindex = 0; int windex = 0; while (mindex < len) { int bytes = getUtf8Len(utfs + mindex); unicodes[windex] = utf8ToUnicode(utfs + mindex, bytes); mindex += bytes; windex ++; } unicodes[windex] = 0; return windex;}\\将unicode数组转换成多字节字符串void unicodesToUtf8s(const unsigned int* unicodes, char* utfs) { sprintf(utfs, "%s", ""); int index = 0; while(unicodes[index] != 0) { char word[4]; unicodeToUtf(unicodes[index], word); sprintf(utfs, "%s%s", utfs, word); index ++; }}
0 0
- 跨平台utf8与unicode的互转
- 展讯平台的utf8转unicode。。
- Unicode与UTF8互转源代码
- 跨平台utf8转unicode研究实现(2)
- 跨平台utf8转unicode研究实现(3)
- 跨平台utf8转unicode研究实现(4)
- GB2312 Unicode转换表实现跨平台utf8转码unicode
- 跨平台:utf8与Unicode、string与wstring相互转换【经验】
- 转贴 utf8 与 unicode的区别
- UNICODE与 UTF8的转换详解
- UNICODE与UTF8,UTF16的含义
- UNICODE与UTF8,UTF16的含义
- unicode utf8 与 ascii的关系
- unicode与utf8之间的转换
- unicode utf8 与 ascii的关系
- 编码 unicode与utf8
- utf8与unicode转换
- utf8与unicode转换
- 3.2.11: jQuery的DOM操作之遍历节点
- 3.2.12: jQuery的DOM操作之CSS-DOM操作
- opengl编译错误解决
- 4.1.1: jQuery事件之加载DOM
- 升级安卓开发环境到Android Studio和Genymotion
- 跨平台utf8与unicode的互转
- 4.1.2: jQuery事件之事件绑定
- 4.1.3: jQuery事件之合成事件
- 基于Python的Web框架
- Linux 下zip包的压缩与解压
- MAX
- 个人征信记录如何消除,求救?推荐q294399932消除
- Weapon
- JavaScript 获取客户端计算机硬件及系统信息