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

最新文章

  1. 正确地组织python项目的结构
  2. D3D的绘制
  3. C语言时间函数
  4. IE PNG格式的图片不现实的的解决方法
  5. Nginx之负载均衡
  6. 使用android.support.design.widget.TabLayout出现java.lang.reflect.InvocationTargetException
  7. Linux文件系统介绍
  8. vim 打开多个文件
  9. PHP扩展开发01:第一个扩展【转】
  10. hdu 4620 搜索
  11. Page Cache buffer Cache
  12. Permutation Recovery(模拟)
  13. 九度OJ 1016 火星A + B 未AC版,整型存储不下
  14. [cf contest246] E - Blood Cousins Return
  15. jqGrid 中文配置 - grid.locale-cn.js 多国语言
  16. January 22nd, 2018 Week 04th Monday
  17. 理解 Redis(9) - Publish Subscribe 消息订阅
  18. Unity 3D UGUI Toggle用法教程
  19. DataTable--数据生成datatable
  20. poj-2777(区间线段树,求种类数模板)

热门文章

  1. treeTable的使用(ajax异步获取数据,动态渲染treeTable)
  2. JavaEE-04 数据源配置
  3. ios之@class
  4. 10MongoDB
  5. Yii1 用commandBuilder方法往数据表中插入多条记录
  6. CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
  7. oracle数字返回为字符串时小时点前面的0缺失的问题
  8. 7. 配置undo表空间
  9. (2) OpenSSL命令
  10. 如何用纯 CSS 创作一种侧立图书的特效