• gbk转utf-8 iconv 编码转换


    linux以下有时候 字符须要进行编码转换(爬虫将gbk转为utf-8编码...)。一般能够选择iconv函数。

    终端以下  输入    

    man 3 iconv

    得到  iconv函数的用法。

    个人看习惯了,msdn文档之后感觉linux以下的文档的看的不是那么爽了。

    使用iconv函数进行转码,一般使用三个函数:iconv_open  、 iconv  、iconv_close三个函数。

    iconv_t iconv_open(const char* tocode,const char* fromcode)

    返回值类似文件句柄的东西。tococode:目标编码,fromcode:来源编码。

    终端以下输入以下命令得到系统支持的编码:

    iconv --list


    然后就是转码函数了:

    size_t iconv(iconv_t cd,             char **inbuf, size_t *inbytesleft,
                char **outbuf, size_t *outbytesleft);

    cd:刚才iconv_open得到的句柄。 inbuf: 须要转码的字符串地址的指针 , inbytesleft:须要转码的长度。outbuf:输出空间 。 outbytesleft:剩余空间

    详细函数内容能够查看这个网页iconv_open iconv iconv_close函数文档


    使用完毕之后。须要关闭之前打开的句柄 :

    int iconv_close(iconv_t cd);

    样例:
    头文件:CTranstlateString.h
    #ifndef CTRANSTLATESTRING_H
    #define CTRANSTLATESTRING_H
    #include <string>
    #include <iostream>
    #include <iconv.h>
    
    class CTranstlateString
    {
    public:
    	CTranstlateString(const char *to_encode , const char *from_encode);
    	const char* Translate(const char* from, size_t flen);  //字符串转换
    	virtual ~CTranstlateString();
    protected:
    private:
    	char* fromstring; //字符串
    	char* tostring;   //
    	size_t fromleng;//带转换字符串预备长度
    	size_t toleng;  //
    	iconv_t handle;
    	const char* InTranlsate();   //正真的字符串函数
    };
    
    #endif // CTRANSTLATESTRING_H
    


    文件:CTranstlateString.cpp
    
    
    #include <string.h>
    #include "CTranstlateString.h"
    using namespace std;
    
    CTranstlateString::CTranstlateString(const char *to_encode , const char *from_encode)
    {
        fromstring = new char[1];
    	fromleng = 1;
        tostring = new char[1];
    	toleng = 1;
    	handle = iconv_open( to_encode , from_encode );
    }
    
    CTranstlateString::~CTranstlateString()
    {
        delete[] fromstring;
    	fromleng = 0;
        delete[] tostring;
    	toleng = 0;
    	iconv_close(handle);
    }
    
    const char* CTranstlateString::Translate(const char* from ,size_t flen)
    {
        if( fromleng < flen+1 )        //将待 编码的字符串 存储起来
        {
            delete[] fromstring;
            fromstring = NULL;
    		fromleng = 0;
            try
            {
                fromstring = new char[flen+1];
    			fromleng = flen + 1;
            }
            catch(...)
            {
                fromstring = NULL;
                fromleng = 0 ;
                return NULL;
            }
        }
    	memset( fromstring , 0 , fromleng );
    	memcpy(fromstring, from, fromleng);
    
    	size_t tlen = flen * 2;
    
    
    	//分类  编码后的字符串空间
        if( toleng < tlen +1 )
        {
            delete[] tostring;
    		tostring = NULL;
    		toleng = 0;
    		try
    		{
    			tostring = new char[tlen + 1];
    			toleng = tlen + 1;
    		}
    		catch (...)
    		{
    			tostring = NULL;
    			toleng = 0;
    			return NULL;
    		}
        }
    	memset(tostring, 0, toleng);
    
    	return InTranlsate();  //字符串转码
    }
    
    const char* CTranstlateString::InTranlsate()
    {
    	size_t outlen = toleng ;
    	char *inbuf = fromstring;
    	char *outbuf = tostring ;
    	size_t inlen = fromleng;
    
    	if ( -1 == iconv( handle ,&inbuf , &inlen , &outbuf , &outlen ) )
    	{
            return "";
    	}
    	return tostring;      //注意这里的返回是重点
    }
    







  • 相关阅读:
    spring boot整合使用mybatis
    spring boot整合使用jdbcTemplate
    spring boot全局捕获异常
    springboot 静态资源访问
    spring boot项目的启动方式
    第一个spring boot项目 springboot-helloworld
    ASP.Net MVC 登录授权验证
    C# mysql 事务处理
    无法删除数据库,因为该数据库当前正在使用"问题解决
    mysql 按照小时去除一段时间内的不同状态数据统计
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6876833.html
Copyright © 2020-2023  润新知