libiconv_sample.c

#include <stdio.h>
#include <malloc.h>
#include "libiconv/iconv.h"
#ifdef _DEBUG
#pragma comment(lib, "libiconv/libiconvd.lib")
#else
#pragma comment(lib, "libiconv/libiconv.lib")
#endif
#define BUFFER_SIZE 10 * 1024 * 1024
int main()
{
const char *toCode = "GBK", *fromCode = "UTF-8";
iconv_t conversionDescriptor = iconv_open(toCode, fromCode);
if ((iconv_t)- == conversionDescriptor)
{
if (errno == EINVAL)
printf("Not supported from %s to %s.\n", fromCode, toCode);
else
printf("Unknown error.\n");
}
else
{
const char *filename = "text_utf-8.txt";
FILE *inputFile = fopen(filename, "r");
if (inputFile)
{
filename = "text_gbk.txt";
FILE *outputFile = fopen(filename, "w");
if (outputFile)
{
char *sourceBuffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
const char *sourcePtr = sourceBuffer;
size_t destinationBufferSize = BUFFER_SIZE * , availableSpaceOfDestinationBuffer = destinationBufferSize;
char *destinationBuffer = (char *)malloc(sizeof(char) * destinationBufferSize), *destinationPtr = destinationBuffer;
size_t numberOfCharactersRead = fread(sourceBuffer, sizeof(char), BUFFER_SIZE, inputFile);
iconv(conversionDescriptor, &sourcePtr, &numberOfCharactersRead, &destinationPtr, &availableSpaceOfDestinationBuffer);
size_t characterCount = destinationBufferSize - availableSpaceOfDestinationBuffer;
destinationBuffer[characterCount] = '\0';
printf(destinationBuffer);
fwrite(destinationBuffer, sizeof(char), characterCount, outputFile);
free(sourceBuffer);
free(destinationBuffer);
fclose(outputFile);
}
else
printf("Cannot open file: %s.\n", filename);
fclose(inputFile);
}
else
printf("Cannot open file: %s.\n", filename);
iconv_close(conversionDescriptor);
}
system("pause");
return ;
}

参考链接:unnonouno/iconvpp: wrapper library of iconv for c++

在C语言中使用libiconv进行编码转换的示例的更多相关文章

  1. ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明

    ASP程序在同一个站点中,如果有UTF-8编码的程序,又有GB2312编码的程序时,在浏览UTF-8编码的页面后,再浏览当前网站GB2312的页面,GB2312编码的页面就会出现乱码 出现这样的问题是 ...

  2. C#中的字符串及其编码转换

    (转自:http://blog.sina.com.cn/s/blog_498eab7d0100et7j.html) 根据查找的System.Text.Encoding类的属性,方法写了如下的转换程序: ...

  3. python中http的一些编码转换

    http的数据需要2种编码解码. 1. url中的特殊字符转换, 比如",', :,//等 python3中通过urllib.parse.quote(..)和urllib.parse.unq ...

  4. python2.7与3.5版本中:编码格式及编码转换

    主要说明编码之间的转换方法 2.7版本: 1 # -*- coding:utf-8 -*- 2 a = "迪丽热巴" 3 a_unicode = a.decode("ut ...

  5. 使用iconv的包装类CharsetConverter进行编码转换的示例

    GitHub地址https://github.com/BuYishi/charset_converter_test charset_converter_test.cpp #include <io ...

  6. JavaScript中基本数据类型之间的转换

    在JavaScript中共有六种数据类型,其中有五种是基本数据类型,还有一种则是引用数据类型.五种基本数据类型分别是:Number 数值类型.String 字符串类型.Boolean 布尔类型, nu ...

  7. 《闲扯Redis四》List数据类型底层编码转换

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...

  8. Qt5字符串编码转换学习

    目录 1.通过Python3示例探索常用字符串编码 UTF8 ANSI Unicode 小结 2.Qt5中使用QTextCodec进行编码转换 小结 1.通过Python3示例探索常用字符串编码 下面 ...

  9. python语言中的编码问题

    在编程的过程当中,常常会遇到莫名其妙的乱码问题.很多人选择出了问题直接在网上找答案,把别人的例子照搬过来,这是快速解决问题的一个好办法.然而,作为一个严谨求实的开发者,如果不从源头上彻底理解乱码产生的 ...

随机推荐

  1. 关于ul中li不对齐的问题

    将li中加入 overflow:hidden;    即可. 同时overflow:auto  可以控制滚动条的出现.

  2. JS事件兼容性

    事件代理的时候,使用事件对象中的srcElement属性,获取触发元素.IE浏览器支持window.event.srcElement , 而firefox支持window.event.target. ...

  3. 最短路中部分点只能从中任意选取K个问题

    题意:给N个点,还有另外m个点(其中只能选K个),求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其他点了),若该点是m个点中的,则count[i]=原来的+1:若不是,则 ...

  4. python学习之-requests模块基础

    安装版本:2.18 模块导入:import requests l  发送请求 发送GET请求: 获取GITHUB的公共时间线 r = requests.get(url='https://api.git ...

  5. 另一篇xtion、kinect选择比较(openni下)

    小小Xtion开箱测评!!2012年03月12日 19:52:55 原文:http://page.renren.com/601107241/note/811764499 ASUS Xtion Pro ...

  6. scanf,fscanf,sscanf的区别----整理

    转自原文 scanf,fscanf,sscanf的区别----整理 scanf 从控制台输入 fscanf 从文件输入 sscanf 从指定字符串输入 1.例:使用scanf函数输入数据. #incl ...

  7. pinpoint 应用性能管理工具安装部署

    原文:http://www.cnblogs.com/yyhh/p/6106472.html pinpoint 安装部署   阅读目录 1. 环境配置 1.1 获取需要的依赖包 1.2 配置jdk1.7 ...

  8. kafka-0.8.1.1总结

    文件夹 一.         基础篇 1.     开篇说明 2.     概念说明 3.     配置说明 4.     znode分类 5.     kafka协议分类 6.     Kafka线 ...

  9. 汉澳sinox通过ndis执行windows驱动程序

    汉澳sinox不仅能通过wine执行windows应用程序.还能通过ndis执行windows驱动程序 汉澳sinox使用 Windows NDIS 驱动程序 详细实现方法是用ndisgen把wind ...

  10. [反汇编练习] 160个CrackMe之023

    [反汇编练习] 160个CrackMe之023. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...