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

charset_converter_test.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include "CharsetConverter.h"
  4. int main()
  5. {
  6. std::string filename("text_utf-8.txt");
  7. std::ifstream ifs(filename, std::ifstream::in);
  8. if (ifs)
  9. {
  10. std::string line, utf8Text;
  11. while (std::getline(ifs, line))
  12. utf8Text.append(line + "\n");
  13. try
  14. {
  15. const std::string &converted = CharsetConverter("GBK", "UTF-8").convert(utf8Text);
  16. std::cout << converted << std::endl;
  17. filename = "text_gbk.txt";
  18. std::ofstream ofs(filename, std::ofstream::out);
  19. if (ofs)
  20. {
  21. ofs.write(converted.c_str(), converted.length());
  22. }
  23. else
  24. std::cerr << "Cannot open file: " << filename << std::endl;
  25. }
  26. catch (const std::string &ex)
  27. {
  28. std::cerr << ex << std::endl;
  29. }
  30. }
  31. else
  32. std::cerr << "Cannot open file: " << filename << std::endl;
  33. std::system("pause");
  34. return ;
  35. }

CharsetConverter.h

  1. #pragma once
  2. #include <iconv/iconv.h>
  3. #include <string>
  4. class CharsetConverter
  5. {
  6. public:
  7. CharsetConverter(const char *toCode, const char *fromCode);
  8. ~CharsetConverter();
  9. std::string convert(const std::string &source) const;
  10. private:
  11. iconv_t conversionDescriptor;
  12. };

CharsetConverter.cpp

  1. #include "CharsetConverter.h"
  2. CharsetConverter::CharsetConverter(const char *toCode, const char *fromCode)
  3. {
  4. conversionDescriptor = iconv_open(toCode, fromCode);
  5. if (reinterpret_cast<iconv_t>(-) == conversionDescriptor)
  6. {
  7. if (errno == EINVAL)
  8. throw std::string("Not supported from " + std::string(fromCode) + " to " + toCode);
  9. else
  10. throw std::string("Unknown error");
  11. }
  12. }
  13. CharsetConverter::~CharsetConverter()
  14. {
  15. iconv_close(conversionDescriptor);
  16. }
  17. std::string CharsetConverter::convert(const std::string &source) const
  18. {
  19. const char *sourcePtr = source.c_str();
  20. size_t sourceByteCount = source.length(), totalSpaceOfDestinationBuffer = sourceByteCount * , availableSpaceOfDestinationBuffer = totalSpaceOfDestinationBuffer;
  21. char *destinationBuffer = new char[totalSpaceOfDestinationBuffer], *destinationPtr = destinationBuffer;
  22. std::string converted;
  23. size_t convertedCharCount;
  24. while (sourceByteCount > )
  25. {
  26. size_t ret = iconv(conversionDescriptor, &sourcePtr, &sourceByteCount, &destinationPtr, &availableSpaceOfDestinationBuffer);
  27. if (static_cast<size_t>(-) == ret)
  28. {
  29. ++sourcePtr;
  30. --sourceByteCount;
  31. }
  32. convertedCharCount = totalSpaceOfDestinationBuffer - availableSpaceOfDestinationBuffer;
  33. }
  34. converted.append(destinationBuffer, convertedCharCount);
  35. delete[] destinationBuffer;
  36. return converted;
  37. }

使用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. redis主从复制以及SSDB主主复制环境部署记录(四)

    redis配置主从和ssdb主主复制 参考:散尽浮华Linux下Redis主从复制以及SSDB主主复制环境部署记录 只做自己查阅. 环境要求:三台虚拟机一台做主,两台做从. 通过SSDB在redis做 ...

  2. 算法复习——cdq分治

    题目: Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要 ...

  3. mybatis学习(一)——mybatis简介

    1.简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...

  4. poj 3461 Oulipo,裸kmp

    传送门 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32373   Accepted: 13093 Desc ...

  5. docker镜像mac下保存路径

    mac下docker的镜像保存位置: /Users/{YourUserName}/Library/Containers/com.docker.docker/Data/com.docker.driver ...

  6. Codeforces 713D Animals and Puzzle(二维ST表+二分答案)

    题目链接 Animals and Puzzle 题意  给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...

  7. springBoot 跨域处理

    首先喝水不忘挖井人,博客参考:https://www.cnblogs.com/nananana/p/8492185.html 方式一:新增一个configration类 或 在Application中 ...

  8. 一张图搞清楚PMBOK所有过程的使用

      很多参加PMP培训的学员大概都会有一个感受,上课时似乎每个知识点都听懂了,大的知识框架也弄明白了,但是所有这些串起来在实践中怎么用呀!说的再直接一点,在考试的时候这些过程和活动是以怎样的逻辑来应用 ...

  9. ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客

    原文:ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客 上一篇文章中我们已经成功的记录了Request部分的信息,现在我们来看下如何记录Res ...

  10. k-svd字典学习,稀疏编码

    1.     K-SVD usage: Design/Learn a dictionary adaptively to betterfit the model and achieve sparse s ...