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. Android圆弧背景

    代码改变世界 Android圆弧背景 <?xml version="1.0" encoding="utf-8"?><shape xmlns:a ...

  2. response.sendRedirect()使用注意事项

    用response.sendRedirect做转向其实是向浏览器发送一个特殊的Header,然后由浏览器来做转向,转到指定的页面,所以用sendRedirect时,浏览器的地址栏上可以看到地址的变化. ...

  3. nmon性能分析工具(生成图表)

    1. nmon性能结果生成报告工具下载地址: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power% ...

  4. 在线预览Word,Excel

    今天在项目中遇到了在线预览word的需求,经过查阅资料与测试发现可以解决问题,特做记录: 方式: http://view.officeapps.live.com/op/view.aspx?src= s ...

  5. 标准C程序设计七---15

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  6. Python入门--8--字符串

    一.创建.修改字符串 str1='呆呆 槑槑 木木 林林' str1[1] #输出呆 str1[2] #输出' ',也就是空值 str1=str[:5]+'插入乖呆 '+str1[5:] #修改字符串 ...

  7. shell的select脚本的简单入门

    shell的select脚本的简单入门 语法:select var in ...;do break;doneecho $var 示例: #/bin/bash echo "what is yo ...

  8. Peter Norvig:十年学会编程

    为啥都想速成? 随便逛一下书店,你会看到<7天自学Java>等诸如此类的N天甚至N小时学习Visual Basic.Windows.Internet的书.我用亚马逊网站的搜索功能,出版年份 ...

  9. spring data jpa 查询部分字段列名无效问题

    spring data jpa原生sql查询问题,我只要表其中的几个字段的值,本以为写个原生sql,拿实体类对象去接没问题 结果列名无效,测试了一下,把返回值类型改成List<Object> ...

  10. Spring Tool Suite (STS) 安装SVN插件

    今天STS安装SVN时遇到很多问题,度娘搜索几个小时才安装成功. 在此记录下安装过程. 我的 STS版本: 安装SVN有两种方式: 方法1:依次选择help->preferences->e ...