GitHub地址https://github.com/BuYishi/charset_converter_test

charset_converter_test.cpp

#include <iostream>
#include <fstream>
#include "CharsetConverter.h"
int main()
{
std::string filename("text_utf-8.txt");
std::ifstream ifs(filename, std::ifstream::in);
if (ifs)
{
std::string line, utf8Text;
while (std::getline(ifs, line))
utf8Text.append(line + "\n");
try
{
const std::string &converted = CharsetConverter("GBK", "UTF-8").convert(utf8Text);
std::cout << converted << std::endl;
filename = "text_gbk.txt";
std::ofstream ofs(filename, std::ofstream::out);
if (ofs)
{
ofs.write(converted.c_str(), converted.length());
}
else
std::cerr << "Cannot open file: " << filename << std::endl;
}
catch (const std::string &ex)
{
std::cerr << ex << std::endl;
}
}
else
std::cerr << "Cannot open file: " << filename << std::endl;
std::system("pause");
return ;
}

CharsetConverter.h

#pragma once
#include <iconv/iconv.h>
#include <string>
class CharsetConverter
{
public:
CharsetConverter(const char *toCode, const char *fromCode);
~CharsetConverter();
std::string convert(const std::string &source) const;
private:
iconv_t conversionDescriptor;
};

CharsetConverter.cpp

#include "CharsetConverter.h"
CharsetConverter::CharsetConverter(const char *toCode, const char *fromCode)
{
conversionDescriptor = iconv_open(toCode, fromCode);
if (reinterpret_cast<iconv_t>(-) == conversionDescriptor)
{
if (errno == EINVAL)
throw std::string("Not supported from " + std::string(fromCode) + " to " + toCode);
else
throw std::string("Unknown error");
}
}
CharsetConverter::~CharsetConverter()
{
iconv_close(conversionDescriptor);
}
std::string CharsetConverter::convert(const std::string &source) const
{
const char *sourcePtr = source.c_str();
size_t sourceByteCount = source.length(), totalSpaceOfDestinationBuffer = sourceByteCount * , availableSpaceOfDestinationBuffer = totalSpaceOfDestinationBuffer;
char *destinationBuffer = new char[totalSpaceOfDestinationBuffer], *destinationPtr = destinationBuffer;
std::string converted;
size_t convertedCharCount;
while (sourceByteCount > )
{
size_t ret = iconv(conversionDescriptor, &sourcePtr, &sourceByteCount, &destinationPtr, &availableSpaceOfDestinationBuffer);
if (static_cast<size_t>(-) == ret)
{
++sourcePtr;
--sourceByteCount;
}
convertedCharCount = totalSpaceOfDestinationBuffer - availableSpaceOfDestinationBuffer;
}
converted.append(destinationBuffer, convertedCharCount);
delete[] destinationBuffer;
return converted;
}

使用iconv的包装类CharsetConverter进行编码转换的示例的更多相关文章

  1. 【Linux基础】iconv命令详解(编码转换)

    对于给定文件把它的内容从一种编码转换成另一种编码. iconv -f GBK -t UTF- file1 -o file2 //将GBK转换为UTF8,输出到file2.没-o那么会输出到标准输出 i ...

  2. PHP iconv 解决utf-8和gb2312编码转换问题

    就一个很简单的函数iconv();但是就是这个函数在网上找了很多例子,都无法成功转换,这是为什么呢?     终于皇天不负有心人,答案还是让我找到了. 网上的都是这样用的   <?php $co ...

  3. iconv 解决utf-8和gb2312编码转换问题

    $content = iconv("utf-8","gb2312//IGNORE",$content); //utf-8转gbk $content = icon ...

  4. 在C语言中使用libiconv进行编码转换的示例

    libiconv_sample.c #include <stdio.h> #include <malloc.h> #include "libiconv/iconv.h ...

  5. iconv字符编码转换

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

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

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

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

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

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

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

  9. php 字符编码转换函数 iconv mb_convert_encoding比较

    在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...

随机推荐

  1. python 列表 字符串 转换

    列表转字符串python中的列表l = ['1','2','3','4']转成str型'1,2,3,4'','.join(l)这个方法,列表里都是字符串的话可以这样用.列表里是整数的情况可以用: &g ...

  2. iOS学习笔记19-地图(一)定位CoreLocation

    一.定位介绍 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.要实现 ...

  3. HDU 1724 Ellipse ——Simpson积分

    [题目分析] 一看题目,直接把椭圆积分起来就可以了嘛. 然后发现椭圆比较难积分,还是算了吧. 用Simpson积分硬上. 大概就是用二次函数去拟合面积. [代码] #include <cstdi ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression(并查集)

    传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...

  5. 【dp】leetcode Best Time to Buy and Sell Stock IV

    https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...

  6. c++ 多线程:线程句柄可以提前关闭,但是线程并没有关闭

    很多程序在创建线程都这样写的:ThreadHandle = CreateThread(NULL,0,.....);CloseHandel(ThreadHandle );1,线程和线程句柄(Handle ...

  7. BS4(BeautifulSoup4)的使用--find_all()篇

    可以直接参考 BS4文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all 注意的是: 1.有些 ...

  8. 在VMWare下为CentOS设置静态IP通过NAT访问外网

    一.背景 安装好的CentOS系统默认是通过DHCP自动分配地址来共享主机的IP以达到访问外网的目的,但是因为莫名的原因无法访问外网.只好改为通过静态IP的方式访问外网. 二.操作步骤 2.1 确认开 ...

  9. HDU 4803 Poor Warehouse Keeper (贪心+避开精度)

    555555,能避开精度还是避开精度吧,,,,我们是弱菜.. Poor Warehouse Keeper Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  10. Python入门--3--操作符

    一.算数操作符 有:+.-.*././/.%.**(幂) a= 3; a = 3+1; #等同于a += 1  这相当与a加一 同样 也可以-.*././/          需要注意的是//是直接舍 ...