/// <summary> /// 判断是否是不带 BOM 的 UTF8 格式 /// </summary> /// <param name="data"></param> /// <returns></returns> public static bool IsUTF8Bytes(byte[] data) { int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 byte curByte; //当前分析的字节. for (int i = 0; i < data.Length; i++) { 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--; } } return true; }
【代码片段】判断UTF8格式文件内容是否带BOM的
猜你喜欢
转载自blog.csdn.net/michelsn/article/details/80287191
今日推荐
周排行