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;
}

最新文章

  1. 使用DiskFileItemFactory 实现文件上传 ,设定缓冲区大小和存放临时文件目录。
  2. sublime test 3 使用及常用插件
  3. Apache Shiro权限框架在SpringMVC+Hibernate中的应用
  4. 单片机C语言开发学习笔记---动态的数码管
  5. 仅显示INPUT下边框
  6. 设置eclipse在linux下提示
  7. A标签中通过href和onclick传递的this对象
  8. 简述ConCurrentHashMap
  9. electron Windows和mac 的菜单栏隐藏
  10. dll 恐怖的代码调整
  11. [转]微擎人人商城m()函数调用model方法
  12. JetBrains 授权服务器(License Server URLS)
  13. discuz论坛 模板修改
  14. java.lang.OutOfMemoryError: GC overhead limit exceeded
  15. phpcms如何给已有的模块添加新功能?
  16. Docker搭建 MySQL 主从复制
  17. VOT工具操作指南(踩过的坑)
  18. [BUAA软工]第零次博客作业---问题回答
  19. Executors提供的四种线程池和自定义线程池
  20. hdu 4107 Gangster(线段树,时间卡得很严)

热门文章

  1. BZOJ 4161 Shlw loves matrixI ——特征多项式
  2. BZOJ 1821 [JSOI2010]Group 部落划分:MST
  3. BZOJ 2463: [中山市选2009]谁能赢呢?【博弈】
  4. 刷题总结——mayan游戏(NOIP2011提高组day2T3)
  5. 算法复习——计算几何基础(zoj1081)
  6. hdu4336 Card Collector(概率DP,状态压缩)
  7. Vmware error:无法获得 VMCI 驱动程序的版本: 句柄无效。
  8. 区间翻转(codevs 3243)
  9. POJ 3099 Go Go Gorelians
  10. Mysql 实现篮球比赛赛程中两支队伍的查询