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. Python升级版本2.6到2.7

    CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value ...

  2. TheBrain8破解方式

    破解文件下载地址:http://rghost.net/51736270 mac破解方式: 我用的MAC 装的8007版本的,今天竟然提示要升级专业版本了.补救方法是,先打开TB,把之前手贱输入的云服务 ...

  3. hdu6059[字典树+思维] 2017多校3

    #include <bits/stdc++.h> using namespace std; typedef long long LL; * ][]; * ]; * ]; ][]; ; LL ...

  4. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

  5. BZOJ 1800: [Ahoi2009]fly 飞行棋【暴力】

    Description 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. Input 第一行为 ...

  6. 洛谷 [P1939] 矩阵加速数列

    矩阵快速幂模版 #include <iostream> #include <cstring> #include <cstdlib> #include <alg ...

  7. 不拖控件的asp.net编程方法——第1回

    以前写的asp.net程序基本上都用了webfrom的控件编写的,当然有个好处就是易入门.快速效率高,但感觉自己这了几个小系统,还是没学到什么东西,感觉心里没底,因为都是封装好的东西,拿来就用的,功能 ...

  8. ORA-01940: cannot drop a user that is currently connected 问题解析

    https://www.linuxidc.com/Linux/2012-12/76448.htm

  9. springboot主要注解及其作用

    1.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  10. 如何使用NSOperations和NSOperationQueues 第二部分

    这篇文章还可以在这里找到 英语 以下是对上面代码的注解: 导入PhotoRecord.h文件,这样你就可以在下载成功后,单独地设置PhotoRecord变量的图片属性(image property). ...