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; //注意这里的返回是重点
}
gbk转utf-8 iconv 编码转换的更多相关文章
- PHP iconv()编码转换函数用法示例
PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...
- iconv编码转换指令
看到一个不错的指令iconv,可以对文件编码进行转换,记录如下: iconv --list 列出所有支持转换的编码 icon -f code1 -t code2 filename -o newfile ...
- iconv编码转换
环境:cocos2dx 3.10 1.vs环境下编译windows版本,需要增加头文件和链接库①cocos2d-x-3.10\external\win32-specific\icon\include② ...
- iconv编码转换报错问题
今天,再由ISO-8859编码格式转化为UTF-8格式过程中,出现报错:iconv: 未知 10304 处的非法输入序列. 问题分析:ISO-8859是英文格式的编码方式,不支持中文,为了解决中文支持 ...
- GBK、UTF8、UNICODE编码转换
string GBKToUTF8(const std::string& strGBK) { , strGBK.c_str(), -, NULL, ); WCHAR * wszUTF8 = ne ...
- UTF-8和GBK编码转换iconv
iconv("GBK", "UTF-8", $str);//将GBK编码转换成UTF8编码
- iconv字符编码转换
转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...
- (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明
之--http://www.jb51.net/article/21451.htm mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍 ...
- PHP下编码转换函数mb_convert_encoding与iconv的使用说明
mb_convert_encoding这个函数是用来转换编码的. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比如你用Zend Studio或Editplus写程序时,用的是gbk编码, ...
随机推荐
- unnamed not found for the web module
intellij idea tomcat 启动报错not found for the web module 使用intellij idea 创建tomcat项目的时候会出现该错误: 启动tomcat的 ...
- MySQL-03 SQL语句设计
学习要点 SQL语句分类 DML语句 DML 查询语句 SQL语句分类 数据操纵语言(DML):用来操纵数据库中数据的命令.包括:SELECT.INSERT.UPDATE.DELETE. 数据定义语言 ...
- HTML基础(二)列表标签
无序列表ul ul标签的格式为 <ul> <li>内容1</li> <li>内容2</li> <li>内容3</li> ...
- C语言中函数参数传递的本质是值传递
数组名做函数参数进行传递时,实际上是是一份该指针的拷贝. 给形参赋予其他值,并不影响实参的值. 类似于: int *p = a; //a为数组名 p = b; //b为数组名 ...
- avalon转成Vue
需求: 旧项目中有部分页面是用avalon写的,avalon存在页面刷新,会出现重复行的问题,数组渲染出现重复行bug, 需要转成vue 总结 记录了一下大概需要改的地方 1. avalon中的ms- ...
- 10. GLOBAL_STATUS 与 SESSION_STATUS
10. GLOBAL_STATUS 与 SESSION_STATUS 注意 从MySQL 5.7.6开始,show_compatibility_56系统变量的值会影响此处描述的表中的可用信息. 有关详 ...
- 我的java web之路(安装)
所有的软件下载完,陪完jdk之后,迎来了一系列的安装工作... 1.安装SQL Server 2005 首先,打开ISS功能,控制面板->程序->打开或关闭windows功能 注意红框内的 ...
- pwntools使用简介3
连接 本地process().远程remote().对于remote函数可以接url并且指定端口. IO模块 下面给出了PwnTools中的主要IO函数.这个比较容易跟zio搞混,记住zio是read ...
- xtu read problem training 3 A - The Child and Homework
The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Code ...
- [Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
传送门 直接搞就行. 注意下表re从零开始,而树状数组搞不了0,所以统一增加一个偏移量1. (话说数据随机是什么鬼?) # include <iostream> # include < ...