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. POJ——2449Remmarguts' Date(A*+SPFA)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26504   Accepted: 7203 ...

  2. bzoj1610 [Usaco2008 Feb]Line连线游戏 几何+暴力

    Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i ...

  3. Codevs 1043 ==洛谷 P1004 方格取数

    题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...

  4. linux-起步

    学习网站: linux中国开源社区 Vmware下载与安装 https://blog.csdn.net/Ywaken/article/details/78839005 https://blog.csd ...

  5. hdu 4952

    Number Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  6. iOS7 毛玻璃效果

    转自:http://prolove10.blog.163.com/blog/static/138411843201391401054305/ 原图:  效果图:  实现:首先需要导入Accelerat ...

  7. AFNetworking实时监测网络连接

    
// 网络变化消息 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(checkNetwork) ...

  8. windows下模拟linux命令的工具 xshell

    windows下模拟linux命令的工具 xshell

  9. js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]

    <script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write( ...

  10. P3378 堆【模板】 洛谷

    https://www.luogu.org/problem/show?pid=3378 题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 ...