iconv文件编码判断转换

继我的前一篇文章《iconv用法,编码转换(一)》 后,补充如何识别一个字符数组里面的文本是否是utf8格式的方法,因为对于非utf8格式的文本也进行iconv()处理的话,会删除掉非utf8文本内容,因此调用iconv()函数前需进行字符集判断。方法如下:

使用int IsTextUTF8(const char* str,unsigned int  length);函数来识别是否为utf8类型:

[cpp]  view plain copy
  1. #include<iconv.h>  
  2. #include<iostream>  
  3. #include<fstream>  
  4.   
  5. using namespace std;  
  6.   
  7. int IsTextUTF8(const char* str,unsigned int  length);  
  8. int main()  
  9. {  
  10.     iconv_t cd = iconv_open("GBK","UTF-8");  
  11.     if(cd == (iconv_t)(-1))  
  12.     {  
  13.         cout<<"Failed";  
  14.     }  
  15.     else   
  16.     {  
  17.         cout<<"Success"<<endl;  
  18.     }  
  19.   
  20.     ifstream fp("1.html");  //1.html为utf8编码格式的文件  
  21.     char * inbuf = new char[1000] ;  
  22.     string s;  
  23.     while(getline(fp,s))  
  24.     {  
  25.         inbuf = (char *)s.c_str();  
  26.         char * in = inbuf;  
  27.         char * outbuf = new char[1000];  
  28.         char * out = outbuf;  
  29.         size_t inlen = 1000;  
  30.         size_t outlen = 1000;  
  31.         int res = IsTextUTF8(in,s.size());  
  32.         if(res)   
  33.         {  
  34.             cout<<"是utf8格式";  
  35.             iconv(cd,&in,&inlen,&out,&outlen);  
  36.         }  
  37.         else   
  38.         {  
  39.             cout<<"不是utf8格式";  
  40.             outbuf = inbuf;  
  41.         }//iconv(cd,&in,&inlen,&out,&outlen);  
  42.         cout<<outbuf;  
  43.     }  
  44.     iconv_close(cd);  
  45.     return 0;  
  46. }  
  47.   
  48.   
  49. int IsTextUTF8(const char* str,unsigned int  length)  
  50. {  
  51.     int i;  
  52.     unsigned long nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节  
  53.     unsigned char chr;  
  54.     int bAllAscii=1; //如果全部都是ASCII, 说明不是UTF-8  
  55.     for(i=0;i<length;i++)  
  56.     {  
  57.         chr= *(str+i);  
  58.         if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx  
  59.             bAllAscii= 0;  
  60.         if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数  
  61.         {  
  62.             if(chr>=0x80)  
  63.             {  
  64.                 if(chr>=0xFC&&chr<=0xFD)  
  65.                     nBytes=6;  
  66.                 else if(chr>=0xF8)  
  67.                     nBytes=5;  
  68.                 else if(chr>=0xF0)  
  69.                     nBytes=4;  
  70.           
  71.         else if(chr>=0xE0)  
  72.                     nBytes=3;  
  73.                 else if(chr>=0xC0)  
  74.                     nBytes=2;  
  75.                 else  
  76.                 {  
  77.                     return 0;  
  78.                 }  
  79.                 nBytes--;  
  80.             }  
  81.         }  
  82.         else //多字节符的非首字节,应为 10xxxxxx  
  83.         {  
  84.             if( (chr&0xC0) != 0x80 )  
  85.             {  
  86.                 return 0;  
  87.             }  
  88.             nBytes--;  
  89.         }  
  90.     }  
  91.     if( nBytes > 0 ) //违返规则  
  92.     {  
  93.         return 0;  
  94.     }  
  95.     if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8  
  96.     {  
  97.         return 0;  
  98.     }  
  99.     return 1;  
  100. }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值