iconv是一个字符集转换函数,原型为:

size_t iconv(iconv_t cd,

             char **inbuf, size_t *inbytesleft,

             char **outbuf, size_t *outbytesleft);

// 传递给do_convert的in_buf,所有字节数(in_buf_size指定)都是可以转换成功的
static int do_convert(iconv_t cd, const char* from, size_t from_size, std::string* to)
{
char* in_buf_ptr = const_cast<char*>(from);
size_t in_bytes_left = from_size;
size_t out_bytes = in_bytes_left*3 + 1;
size_t out_bytes_left = out_bytes;
std::string out(out_bytes_left, '\0');
char* out_buf_start = const_cast<char*>(out.c_str());
char* out_buf_ptr = out_buf_start; int bytes = iconv(cd, &in_buf_ptr, &in_bytes_left, &out_buf_ptr, &out_bytes_left);
if (-1 == bytes)
return errno; to->assign(out_buf_start, out_bytes-out_bytes_left);
return 0;
} // 可忽略不能转换的部分,
// 也可以在结果中保留不能被转换的部分
// 详细实现可以浏览:
// https://github.com/eyjian/mooon/blob/master/common_library/src/utils/charset_utils.cpp
void CCharsetUtils::convert(const std::string& from_charset, const std::string& to_charset,
const std::string& from, std::string* to,
bool ignore_error, bool skip_error) throw (CException)
{
std::string result; // 用来保存处理后的内容
char* in_buf = const_cast<char*>(from.c_str());
size_t in_bytes = from.size(); // 需要处理的总字节数
size_t in_bytes_left = in_bytes; // 剩余的未被处理的字节数
iconv_t cd = iconv_open(to_charset.c_str(), from_charset.c_str()); if ((iconv_t)(-1) == cd)
{
THROW_EXCEPTION(strerror(errno), errno);
}
while (in_bytes_left > 0)
{
int errcode;
size_t out_bytes = in_bytes_left * 3 + 1; // 保证足够大
size_t out_bytes_left = out_bytes;
std::string out(out_bytes_left, '\0');
char* out_buf = const_cast<char*>(out.c_str());
char* out_buf_start = out_buf;
char* in_buf_start = in_buf; // 如果成功,返回值bytes为0
// 如果成功,in_buf指向in的结尾符,即'\0',同时in_bytes_left值为0
// 如果失败,in_buf指向未能转换的起始地址,而in_bytes_left值为剩余的未被转换的(可能含有可转换的)字节数
// 如果成功,则out_bytes-out_bytes_left值为转换后的字节数
// 如果成功,则out_buf_start存储了被转换后的结果,有效长度为out_bytes-out_bytes_left
int bytes = iconv(cd, &in_buf, &in_bytes_left, &out_buf, &out_bytes_left);
if (bytes != -1)
{
result.append(out_buf_start, out_bytes-out_bytes_left);
break;
}
else if (!ignore_error)
{
errcode = errno;
iconv_close(cd);
THROW_EXCEPTION(strerror(errcode), errcode);
}
else
{
// EILSEQ An invalid multibyte sequence has been encountered in the input.
// EINVAL An incomplete multibyte sequence has been encountered in the input.
if ((errno != EINVAL) &&
(errno != EILSEQ))
{
// E2BIG There is not sufficient room at *outbuf.
errcode = errno;
iconv_close(cd);
THROW_EXCEPTION(strerror(errcode), errcode);
}
else
{
// in_buf之前部分是可以转换的
if (in_buf != in_buf_start)
{
std::string str;
errcode = do_convert(cd, in_buf_start, in_buf-in_buf_start, &str);
if (errcode != 0)
{
iconv_close(cd);
THROW_EXCEPTION(strerror(errcode), errcode);
} result.append(str);
} // skip_error决定未能被转换的是否出现在结果当中
if (!skip_error)
{
result.append(in_buf, 1);
} // 往前推进
--in_bytes_left; // 将导致while语句结束
++in_buf;
}
}
} if (-1 == iconv_close(cd))
{
THROW_EXCEPTION(strerror(errno), errno);
} // 不能直接使用to,因为to可能就是from
*to = result;
} void CCharsetUtils::gbk_to_utf8(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
convert("gbk", "utf-8", from, to, ignore_error, skip_error);
} void CCharsetUtils::utf8_to_gbk(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
convert("utf-8", "gbk", from, to, ignore_error, skip_error);
} void CCharsetUtils::gb2312_to_utf8(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
convert("gb2312", "utf-8", from, to, ignore_error, skip_error);
} void CCharsetUtils::utf8_to_gb2312(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
convert("utf-8", "gb2312", from, to, ignore_error, skip_error);
}

iconv用法解读的更多相关文章

  1. CMake的含义和用法解读

    什么是 CMake 你或许听过好几种 Make 工具,例如 GNU Make ,QT 的 qmake ,微软的 MS nmake,BSD Make(pmake),Makepp,等等.这些 Make 工 ...

  2. 学习AngularJs:Directive指令用法

    跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...

  3. const和typedef的常见用法详解

    一.说说const 一般而言,const主要是用来防止定义的对象再次被修改,定义对象变量时要初始化变量. 常见用法如下: 1.用于定义常量变量,这样这个变量在后面就不可以再被修改 const int ...

  4. iconv命令

    iconv 用法: Usage: iconv [OPTION...] [FILE...] Convert encoding of given files from one encoding to an ...

  5. ralitive absolute

    3.relative与absolute的主要区别: 首先,是上面已经提到过的在正常流中的位置存在与否. 其次,relative定位的层总是相对于其最近的父元素,无论其父元素是何种定位方式.如图3: 图 ...

  6. php生成CSV格式(转)

    参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...

  7. Linux下windows中文文本文件乱码问题

    table of content: 乱码问题 用gedit选择正确的字符编码打开文件 文件转码 总结 §乱码 Fedora安装时默认用UTF-8字符编码方式, 这么做有国际化的好处(和很多用utf-8 ...

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

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

  9. linux下iconv()函数的用法(转载并修改)

    linux shell 配置文件中默认的字符集编码为UTF-8 .UTF-8是unicode的一种表达方式,gb2312是和unicode都是字符的编码方式,所以说gb2312跟utf-8的概念应该不 ...

随机推荐

  1. Window平台下React Native 开发环境搭建

    1. 安装Node.js 2. 安装react-native-cli 命令行工具 npm install -g react-nativew-cli 3. 创建项目 $ react-native ini ...

  2. 用PHP收发邮件

    注:在WinForm下怎么试都不好使,最后没办法换成了PHP下,一试就好使了. 这里用到了phpmailer组件,网上有下. 转:phpnow支持ssl的方法 原文出处:http://blog.163 ...

  3. scrollWidth,clientWidth,offsetWidth的区别 ---转载的

    转载自博客:http://www.cnblogs.com/kongxianghai/p/4192032.html 通过一个demo测试这三个属性的差别. 说明: scrollWidth:对象的实际内容 ...

  4. zufeoj 1018 阶乘第k位是多少(sprintf)

    题目描述     n的阶乘定义为n!=1*2*3*……*n  如3!=6     n!通常最后会有很多0,如5!=120  最后有一个0,现在统计n!去除末尾的0后,最后k位是多少 输入     第一 ...

  5. Python链表与反链表

    # -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...

  6. git grade 版本下载及安装

    Git 2.11.1x64下载 gradle各版本下载地址 1. Git安装与配置 Gradle 用法总结

  7. ROS创建Web代理(Web proxy)给QQ使用HTTP代理

    使用Web代理可以提高网页的访问速度,因为访问的数据会存储在内存或是硬盘中,就会直接从代理服务器中读取.同时,为了提高网络访问的安全性,可以给Web代理服务器设置相应的权限,使它的安全性得到提高. 下 ...

  8. 2017CCSP总结——失败(铜)

    这次比赛,算是铩羽而归.尽管是第一次出去打比赛,在经验方面略显不足,但是,归根到底,我这次比赛打的很失败.包括我们学校去的,打的也不好,可以说是全体翻车.真的很对不起带我们去的老师.>_< ...

  9. 客户端调用wcf服务,如何提高调用性能

    IO调用服务 1.使用using(每次自动释放) ; i < ; i++) { var watch = new Stopwatch(); watch.Start(); using (var cl ...

  10. asp.net,C#操作数据库DataTable关于空null的判断

    double d=0;if(!Convert.IsDBNull(DataTable.Rows[i][m])){    string str=DataTable.Rows[i][m].ToString( ...