识别文件编码
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
bool TextEncode(const char *fPath) { char srcBuff[1024]; char header[2]; unsigned char uniTxt[] = {0xFF, 0xFE}; // Unicode file header unsigned char endianTxt[] = {0xFE, 0xFF}; // Unicode big endian file header unsigned char utf8Txt[] = {0xEF, 0xBB, 0xBF}; // UTF_8 file header int len = 0; int ascii = 0; FILE *pFile; pFile = fopen(fPath, "rb"); if (NULL == pFile) { return false; } // ASCII range(0~127) while (1) { len = fread(srcBuff, 1, 1024, pFile); if (0 == len) { break; } for (int i=0; i<len; i++) { header[0] = srcBuff[0]; header[1] = srcBuff[1]; header[2] = srcBuff[2]; if (srcBuff[i]<0 || srcBuff[i]>127) { ascii++; } } } if (0 == ascii) // ASCII file { printf("ASCII text\n"); } else if ((2 == ascii) && (0 == memcmp(header, uniTxt, sizeof(uniTxt)))) // Unicode file { printf("Unicode text\n"); } else if ((2 == ascii) && (0 == memcmp(header, endianTxt, sizeof(endianTxt)))) // Unicode big endian file { printf("Unicode big endian text\n"); } else if ((3 == ascii) && (0 == memcmp(header, utf8Txt, sizeof(utf8Txt)))) // UTF-8 file { printf("UTF-8 text\n"); } else { printf(" Unknow\n"); } fclose(pFile); return true; } string filename = “c:\\Default.asp”; ifstream fin( filename.c_str(),ios::binary); if( !fin ) { cout << “打开文件” << filename << “出错” << endl; //exit(-1); } else { byte bytes[3]; fin.read((char *)&bytes,sizeof bytes); if(bytes[0] == 0xEF&& bytes[1] == 0xBB && bytes[2] == 0xBF) { cout <<”UTF8″<<endl; }else { cout <<”GB2312″<<endl; } } fin.close(); int IsTextUTF8(char* str,ULONGLONG length) { int i; DWORD nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节 UCHAR chr; BOOL bAllAscii=TRUE; //如果全部都是ASCII, 说明不是UTF-8 for(i=0;i<length;i++) { chr= *(str+i); if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx bAllAscii= FALSE; if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数 { if(chr>=0x80) { if(chr>=0xFC&&chr<=0xFD) nBytes=6; else if(chr>=0xF8) nBytes=5; else if(chr>=0xF0) nBytes=4; else if(chr>=0xE0) nBytes=3; else if(chr>=0xC0) nBytes=2; else { return FALSE; } nBytes--; } } else //多字节符的非首字节,应为 10xxxxxx { if( (chr&0xC0) != 0x80 ) { return FALSE; } nBytes--; } } if( nBytes > 0 ) //违返规则 { return FALSE; } if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8 { return FALSE; } return TRUE; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=317