[转]识别不带BOM(无签名)的UTF-8文件
类别:c++
状态:游客可见,可回,会员可关联(良好)
阅读:5544
评论:0
时间:Feb. 3, 2013, 1:53 p.m.
关键字:UTF-8
带有签名的UTF-8文件可以通过读取BOM轻松识别, 而不带签名的UFT-8文件只有通过UTF-8编码的规则来尝试辨别。
先来看看UTF-8编码是如何实现UNICODE字符的:
UNICODE | UTF-8 |
00000000 - 0000007F | 0xxxxxxx |
00000080 - 000007FF | 110xxxxx 10xxxxxx |
00000800 - 0000FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
00010000 - 001FFFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
00200000 - 03FFFFFF | 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
04000000 - 7FFFFFFF | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
从上表可以看出如果以个UTF-8字符是以2个或2个以上的字节组成, 则首字节前有多个连续的值为1的比特位, 随后是一个值为0的比特位。
根据这一特点我们可以尝试识别判断一个文件是否为UFT-8格式。
代码如下:
//data为从文件读取的字节数组 bool IsUTF8Bytes(unsigned char data[], long iLength) { int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 for (int i = 0; i < iLength; i++) { unsigned char curByte = data[i]; //当前分析的字节. if (charByteCounter == 1) { if (curByte >= 0x80) { //判断当前 while (((curByte <<= 1) & 0x80) != 0) { charByteCounter++; } //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X if (charByteCounter == 1 || charByteCounter > 6) { return false; } } } else { //若是UTF-8 此时第一位必须为1 if ((curByte & 0xC0) != 0x80) { return false; } charByteCounter--; } } if (charByteCounter > 1) { // "非预期的byte格式" return false; } return true; }
下面好理解:
bool IsUTF8Bytes(unsigned char data[], long iLength) { for (int i = 0; i < iLength;) { unsigned char curByte = data[i]; //当前分析的字节. i++; if (curByte < 0x80) { } else if ((curByte & 0xFE) == 0xFC) { for (int j=0; j<5; i++, j++) { curByte = data[i]; if ((curByte & 0xC0) == 0X80) { // "非预期的byte格式" return false; } } } else if ((curByte & 0xFC) == 0xF8) { for (int j=0; j<4; i++, j++) { curByte = data[i]; if ((curByte & 0xC0) == 0X80) { // "非预期的byte格式" return false; } } } else if ((curByte & 0xF8) == 0xF0) { for (int j=0; j<3; i++, j++) { curByte = data[i]; if ((curByte & 0xC0) == 0X80) { // "非预期的byte格式" return false; } } } else if ((curByte & 0xF0) == 0xE0) { for (int j=0; j<2; i++, j++) { curByte = data[i]; if ((curByte & 0xC0) == 0X80) { // "非预期的byte格式" return false; } } } else if ((curByte & 0xE0) == 0xC0) { for (int j=0; j<1; i++, j++) { curByte = data[i]; if ((curByte & 0xC0) == 0X80) { // "非预期的byte格式" return false; } } } else { // "非预期的byte格式" return false; } } return true; }
需要注意的是,一些比较特殊的字符用此方法无法正确识别是否为UTF8格式, 传说中的微软屏蔽联通就是此问题
希望此文对大家有帮助
附:
1.UTF-8的相关基础知识可以前往这里http://www.codeguru.com/Cpp/misc/misc/multi-lingualsupport/article.php/c10451/
操作: