iconv用法解读
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用法解读的更多相关文章
- CMake的含义和用法解读
什么是 CMake 你或许听过好几种 Make 工具,例如 GNU Make ,QT 的 qmake ,微软的 MS nmake,BSD Make(pmake),Makepp,等等.这些 Make 工 ...
- 学习AngularJs:Directive指令用法
跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...
- const和typedef的常见用法详解
一.说说const 一般而言,const主要是用来防止定义的对象再次被修改,定义对象变量时要初始化变量. 常见用法如下: 1.用于定义常量变量,这样这个变量在后面就不可以再被修改 const int ...
- iconv命令
iconv 用法: Usage: iconv [OPTION...] [FILE...] Convert encoding of given files from one encoding to an ...
- ralitive absolute
3.relative与absolute的主要区别: 首先,是上面已经提到过的在正常流中的位置存在与否. 其次,relative定位的层总是相对于其最近的父元素,无论其父元素是何种定位方式.如图3: 图 ...
- php生成CSV格式(转)
参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...
- Linux下windows中文文本文件乱码问题
table of content: 乱码问题 用gedit选择正确的字符编码打开文件 文件转码 总结 §乱码 Fedora安装时默认用UTF-8字符编码方式, 这么做有国际化的好处(和很多用utf-8 ...
- PHP iconv()编码转换函数用法示例
PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...
- linux下iconv()函数的用法(转载并修改)
linux shell 配置文件中默认的字符集编码为UTF-8 .UTF-8是unicode的一种表达方式,gb2312是和unicode都是字符的编码方式,所以说gb2312跟utf-8的概念应该不 ...
随机推荐
- 1、hadoop HA分布式集群搭建
概述 hadoop2中NameNode可以有多个(目前只支持2个).每一个都有相同的职能.一个是active状态的,一个是standby状态的.当集群运行时,只有active状态的NameNode是正 ...
- c#中变量的作用域
C#中的作用域和javascript中的作用域还是有区别的.呵呵 class Person { /* *确定C#变量作用域的2个规则. 1.类的字段所处的作用域等同于该字段所属类所在的作用域; * 2 ...
- 常见报表的JS代码
1.合并单元格 这个表格在报表里面算是比较典型的 1.里面的表格的列标题现在是写死的,其实这些可以通过配置进行 2.至于如果表头要进行合并(这种需求比较少,也比较容易实现) 3.至于统计:最好在后台按 ...
- webkit内核的浏览器为什么removeAttribute('style')会失效?
做了一些研究,应该算是理清了问题. 首先,我们在这里常说的「属性」(attributes)其实分为两种:内容属性(content attributes)以及 IDL 属性(IDL attributes ...
- Ubuntu 升级内核版本
查看当前内核版本 sch01ar@ubuntu:~$ uname -r Ubuntu 内核地址:https://kernel.ubuntu.com/~kernel-ppa/mainline/ 打开这个 ...
- Selenium Webdriver——Table类封装
WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...
- html5 filereader 读取图片信息
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 图像特征与描述子(直方图, 聚类, 边缘检测, 兴趣点/关键点, Harris角点, 斑点(Blob), SIFI, 纹理特征)
1.直方图 用于计算图片特征,表达, 使得数据具有总结性, 颜色直方图对数据空间进行量化,好比10个bin 2. 聚类 类内对象的相关性高 类间对象的相关性差 常用算法:kmeans, EM算法, m ...
- linux下实时查看log
1.先切换到:cd usr/local/tomcat5/logs2.tail -f catalina.out3.这样运行时就可以实时查看运行日志了 Ctrl+c 是退出tail命令. 顺便讲一下lin ...
- Linux下的终端快捷键
今天才发现Linux下的终端有这么多好用的快捷键. Shift+Ctrl+T:新建标签页 Shift+Ctrl+W:关闭标签页 Ctrl+PageUp:前一标签页 Ctrl+PageDown:后一标签 ...