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; //注意这里的返回是重点
}

gbk转utf-8 iconv 编码转换的更多相关文章

  1. PHP iconv()编码转换函数用法示例

    PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...

  2. iconv编码转换指令

    看到一个不错的指令iconv,可以对文件编码进行转换,记录如下: iconv --list 列出所有支持转换的编码 icon -f code1 -t code2 filename -o newfile ...

  3. iconv编码转换

    环境:cocos2dx 3.10 1.vs环境下编译windows版本,需要增加头文件和链接库①cocos2d-x-3.10\external\win32-specific\icon\include② ...

  4. iconv编码转换报错问题

    今天,再由ISO-8859编码格式转化为UTF-8格式过程中,出现报错:iconv: 未知 10304 处的非法输入序列. 问题分析:ISO-8859是英文格式的编码方式,不支持中文,为了解决中文支持 ...

  5. GBK、UTF8、UNICODE编码转换

    string GBKToUTF8(const std::string& strGBK) { , strGBK.c_str(), -, NULL, ); WCHAR * wszUTF8 = ne ...

  6. UTF-8和GBK编码转换iconv

    iconv("GBK", "UTF-8", $str);//将GBK编码转换成UTF8编码

  7. iconv字符编码转换

    转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...

  8. (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明

    之--http://www.jb51.net/article/21451.htm mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍 ...

  9. PHP下编码转换函数mb_convert_encoding与iconv的使用说明

    mb_convert_encoding这个函数是用来转换编码的. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比如你用Zend Studio或Editplus写程序时,用的是gbk编码, ...

随机推荐

  1. Selenium3+python自动化008-操作浏览器基本方法

    一.打开网站1.第一步:从selenium里面导入webdriver模块2.打开Firefox浏览器(Ie和Chrome对应下面的)3.打开百度网址二.页面刷新1.有时候页面操作后,数据可能没及时同步 ...

  2. 11. GLOBAL_VARIABLES 与 SESSION_VARIABLES

    11. GLOBAL_VARIABLES 与 SESSION_VARIABLES 注意 从MySQL 5.7.6开始,show_compatibility_56系统变量的值会影响此处描述的表中的可用信 ...

  3. CSS3--- 颜色

    1.RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法:color:rgba(R,G,B, ...

  4. 条款31:将文件间的编译依存关系降至最低(Minimize compilation dependencies between files)

    NOTE1: 1.支持“编译依存性最小化”的一般构想是:相依于声明式,不要相依于定义式.基于此构想的两个手段是Handle classes 和 Interface classes. 2.程序库头文件应 ...

  5. @RestController 与 @Controller 注解区别

    文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html @RestController注解相当于@ResponseBody + @Controller ...

  6. tornado框架基础08-sqlalchemy表关系和简单登录注册

    01 一对一表关系 Module 需要先创建对应的 Module ,这里采用之前建立好的 User 和 UserDetails relationship from sqlalchemy.orm imp ...

  7. Python之阻塞IO模型与非阻塞IO模型

    Python之阻塞IO模型与非阻塞IO模型 IO模型 1 阻塞IO: 全程阻塞 2 非阻塞IO: 发送多次系统调用: 优点:wait for data时无阻塞 缺点:1 系统调用太多 2 数据不是实时 ...

  8. 生成优惠券,并将优惠券存入Mysql

    #coding:utf-8 import random import string import MySQLdb def gen_charint(filename, width =4, num=5): ...

  9. Python爬虫-爬取京东商品信息-按给定关键词

    目的:按给定关键词爬取京东商品信息,并保存至mongodb. 字段:title.url.store.store_url.item_id.price.comments_count.comments 工具 ...

  10. luogu2894 [USACO08FEB]酒店Hotel

    跟线段树求区间最值一样每个节点维护左边开始的最大连续空房间数.右边开始的最大连续空房间数.这个区间内的最大连续空房间数 #include <iostream> #include <c ...