编码转换(gbk2utf8,by c++),判断编码格式是否为utf8

8 篇文章 0 订阅

#include <iconv.h>
#include
#include

bool  IsTextUTF8(const char* str,int length)  
{  
    int nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节  
    unsigned char chr;  
    bool bAllAscii=true; //如果全部都是ASCII, 说明不是UTF-8  
    for(int 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;  
}
string  CodeConvert(char *source_charset, char *to_charset, const string& sourceStr) //sourceStr是源编码字符串
{
	iconv_t cd = iconv_open(to_charset, source_charset);//获取转换句柄,void*类型
	if (cd == 0)
		return "iconv  open error"; 
    size_t inlen = sourceStr.size();
	size_t outlen = 255;
	char* inbuf = (char*)sourceStr.c_str();
	char outbuf[255];//这里实在不知道需要多少个字节,这是个问题
	//char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配内存,否则转换失败,猜测跟iconv函数有关
	memset(outbuf, 0, outlen);
	char *poutbuf = outbuf; //多加这个转换是为了避免iconv这个函数出现char(*)[255]类型的实参与char**类型的形参不兼容
	if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)
		return "iconv error"; 
    std::string strTemp(outbuf);//此时的strTemp为转换编码之后的字符串
	iconv_close(cd);
	return strTemp;
}
//gbk转UTF-8  
string GbkToUtf8(const std::string& strGbk)// 传入的strGbk是GBK编码 
{
    if(IsTextUTF8(strGbk.c_str(),strlen(strGbk.c_str()))==false)
	    return CodeConvert("gb2312", "utf-8",strGbk);
    else
        return strGbk;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值