自动检测汉字GB18030编码与UTF-8编码
2012-03-13 17:43
369 查看
先看看汉字的GB18030编码与UTF-8编码范围
GB18030
第1位:0x81 ~ 0xFE 1000 0001 ~ 1111 1110
第2位:0x40 ~ 0x7E 0100 0000 ~ 0111 1110
或 者:0x80 ~ 0xFE 1000 0000 ~ 1111 1110
UTF-8
第1位:0xE0 ~ 0xEF 1110 0000 ~ 1110 1111
第2位:0x80 ~ 0xBF 1000 0000 ~ 1011 1111
第3位:0x80 ~ 0xBF 1000 0000 ~ 1011 1111
想到如下特征来识别汉字:
1. 如果第1位是0就不需要判断的,一定是ASCII字符。
2. 如果第1位是1开头的,第2位是0开头的,一定是GB编码。
3. 如果第1位是非1110开头的,则一定是GB编码。
4. 多做几个汉字判断。
考虑到判断效率,简要写了如下代码做判断即可达到基本效果:
写一个测试例子来测试一下:
编译:
运行结果:
参考文献:
UTF-8编码检测失败特例
http://www.kuqin.com/language/20071201/2740.html
UTF-8文件的Unicode签名BOM(Byte Order Mark)问题
http://blog.csdn.net/thimin/article/details/1724393
UTF-8
http://zh.wikipedia.org/zh-cn/UTF-8
GB 18030
http://zh.wikipedia.org/wiki/GB_18030
下载:
http://download.csdn.net/detail/firstboy0513/4137551
GB18030
第1位:0x81 ~ 0xFE 1000 0001 ~ 1111 1110
第2位:0x40 ~ 0x7E 0100 0000 ~ 0111 1110
或 者:0x80 ~ 0xFE 1000 0000 ~ 1111 1110
UTF-8
第1位:0xE0 ~ 0xEF 1110 0000 ~ 1110 1111
第2位:0x80 ~ 0xBF 1000 0000 ~ 1011 1111
第3位:0x80 ~ 0xBF 1000 0000 ~ 1011 1111
想到如下特征来识别汉字:
1. 如果第1位是0就不需要判断的,一定是ASCII字符。
2. 如果第1位是1开头的,第2位是0开头的,一定是GB编码。
3. 如果第1位是非1110开头的,则一定是GB编码。
4. 多做几个汉字判断。
考虑到判断效率,简要写了如下代码做判断即可达到基本效果:
/* * Get the character code type. (UTF-8 or GB18030) * @param s the string to be operator. * @return return the code type. (1 means UTF-8, 0 for GB18030, -1 for error) */ int get_character_code_type(const char* s) { if (NULL == s) { return -1; } int i = 0; for(; s[i] != '\0'; i++) { // ASCII character. if (!(s[i] & 0x80)) { continue; } // Hanzi utf-8 code possiable. else if(!( (s[i] & 0xF0) ^ 0xE0) && s[i+1] && !( (s[i+1] & 0xC0) ^ 0x80) && s[i+2] && !( (s[i+2] & 0xC0) ^ 0x80)) { return 1; } // Not a UTF-8 code. else { return 0; } } return -1; }
写一个测试例子来测试一下:
#include "char_code.h" #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> int main(int argc, char* argv[]) { if (argc < 2) { printf("%s [file_path]\n", argv[0]); return -1; } // open file and read buf. int f = open(argv[1], O_RDONLY); if ( -1 == f ) { fprintf(stderr, "file %s open failed.\n", argv[1]); return -1; } char buf[1024] = {0}; read(f, buf, 1023); int ret = get_character_code_type(buf); fprintf(stdout, "char code type = %s\n", (ret == 1 ? "UTF-8" : "GB18030")); // close file. if ( 0 != close(f)) { fprintf(stderr, "file %s close failed.\n", argv[1]); return -1; } return 0; }
编译:
gcc test.c -o test -O0 -g3 -Wall
运行结果:
$ ./test gb18030.txt char code type = GB18030 $ ./test utf8.txt char code type = UTF-8
参考文献:
UTF-8编码检测失败特例
http://www.kuqin.com/language/20071201/2740.html
UTF-8文件的Unicode签名BOM(Byte Order Mark)问题
http://blog.csdn.net/thimin/article/details/1724393
UTF-8
http://zh.wikipedia.org/zh-cn/UTF-8
GB 18030
http://zh.wikipedia.org/wiki/GB_18030
下载:
http://download.csdn.net/detail/firstboy0513/4137551
相关文章推荐
- 支持生僻字且自动识别utf-8编码的php汉字转拼音类
- 支持生僻字且自动识别utf-8编码的php汉字转拼音类
- 在ASP中自动检测文件的编码格式,支持UTF-8、GB2312、UTF-7、Unicode、Unicode Swap等格式的检测
- 1个汉字在UTF-8编码占3个字节
- 批量修改文本文件编码GB18030为UTF-8
- c#字符编码,System.Text.Encoding类,字符编码大全:如Unicode编码、GB18030、UTF-8,UTF-7,GB2312,ASCII,UTF32,Big5
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- 【转】IDEA IDEA 9 快捷键 utf-8 编码 参数设置 编码设置 设置配置 自动保存设置 优化设置
- PHP 检测字符串的编码是否是UTF-8编码的函数 check_utf8() 准确率最高的!
- PHP中不同编码的汉字占的字节数不同gbk,GBK,UTF-8,utf-8
- js设置汉字为utf-8的编码
- 常用字符集编码详解:ASCII 、GB2312、GBK、GB18030、unicode、UTF-8
- hibernate自动建表采用UTF-8字符编码
- unicode utf-8 gb18030 gb2312 gbk各种编码对比 [转]
- 常用字符集编码详解:ASCII+、GB2312、GBK、GB18030、unicode、UTF-8
- UTF-8编码中BOM的检测与删除
- UTF-8、GB2312、GB18030、GBK和BIG5等字符集编码范围的具体说明
- 常用字符集编码详解:ASCII 、GB2312、GBK、GB18030、unicode、UTF-8
- php自动识别文件编码并转换为UTF-8的方法
- UTF-8,Unicode,GBK,希腊字母读法,ASCII码表,HTTP错误码,URL编码表,HTML特殊字符,汉字编码简明对照表