依赖库libiconv,libiconv库的交叉编译不做描述,网上很多

 #include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "iconv.h"
#include "eventlist.h" static int ChangeCode( const char* pFromCode, const char* pToCode, const char* pInBuf,
size_t* piInLen, char* pOutBuf, size_t* piOutLen )
{
int iRet;
char **pin = &pInBuf;
char **pout = &pOutBuf;
iconv_t hIconv; //printf("%s: outlen=%d, inlen=%d\n", __FUNCTION__, *piOutLen, *piInLen); //打开字符集转换
hIconv = iconv_open( pToCode, pFromCode );
if ( - == (int)hIconv )
{
perror("iconv_open");
return -;
}
//开始转换
printf("%s: 1 outlen=%d\n", __FUNCTION__, *piOutLen);
iRet = iconv( hIconv, pin, piInLen, pout, piOutLen );
if ( - == iRet )
{
perror("iconv");
return -;
}
printf("%s: 2 outlen=%d\n", __FUNCTION__, *piOutLen); //关闭字符集转换
iconv_close( hIconv ); **pout = '\0';
return iRet;
} static int MAIN_UTFToGb2312(char * pcFrom, char * pcTo, int iMaxToLen)
{
char * psInBuf = NULL;
char * psOutBuf = NULL;
unsigned int iInLen = ;
unsigned int iOutLen = ;
int iRet; iInLen = strlen(pcFrom)+;
psInBuf = (char *)malloc(iInLen);
if ( NULL == psInBuf )
{
return ;
}
memset(psInBuf, 0x0, iInLen);
memcpy(psInBuf, pcFrom, iInLen); iOutLen = iMaxToLen;
psOutBuf = (char *)malloc(iOutLen);
if ( NULL == psOutBuf )
{
free(psInBuf);
psInBuf = NULL;
return ;
}
memset(psOutBuf, 0x0, iOutLen); iRet = ChangeCode( "utf-8", "gb2312", psInBuf, &iInLen, psOutBuf, &iOutLen );
//iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen);
if ( != iRet )
{
printf("ChangeCode: Error\n");
//return 0;
}
memcpy(pcTo, psOutBuf, iOutLen);
printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); free(psInBuf);
psInBuf = NULL;
free(psOutBuf);
psOutBuf = NULL; return iOutLen; } static int MAIN_GB2312ToUTF(char * pcFrom, char * pcTo, int iMaxToLen)
{
char * psInBuf = NULL;
char * psOutBuf = NULL;
unsigned int iInLen = ;
unsigned int iOutLen = ;
int iRet; iInLen = strlen(pcFrom)+;
psInBuf = (char *)malloc(iInLen);
if ( NULL == psInBuf )
{
return ;
}
memset(psInBuf, 0x0, iInLen);
memcpy(psInBuf, pcFrom, iInLen); iOutLen = iMaxToLen;
psOutBuf = (char *)malloc(iOutLen);
if ( NULL == psOutBuf )
{
free(psInBuf);
psInBuf = NULL;
return ;
}
memset(psOutBuf, 0x0, iOutLen); iRet = ChangeCode( "gb2312", "utf-8", psInBuf, &iInLen, psOutBuf, &iOutLen );
//iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen);
if ( != iRet )
{
printf("ChangeCode: Error\n");
//return 0;
}
memcpy(pcTo, psOutBuf, iOutLen);
printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); free(psInBuf);
psInBuf = NULL;
free(psOutBuf);
psOutBuf = NULL; return iOutLen; } int main()
{
char strUTF[]={
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0x00, 0x00, 0x00
};
char chTmpStr[];
int len = ; memset(chTmpStr, 0x0, );
MAIN_UTF2Gb2312(strUTF, chTmpStr, );
printf("Main: change=%s\n", chTmpStr); return ;
}

C语言-字符编码转换:UTF与GB2312的更多相关文章

  1. php字符编码转换之gb2312转为utf8(转)

    在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ...

  2. iconv字符编码转换

    转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...

  3. Char Tools,方便的字符编码转换小工具

    工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...

  4. php 字符编码转换函数 iconv mb_convert_encoding比较

    在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...

  5. C语言字符编码处理

    一.字符编码识别 1.简介 uchardet是一个开源的用于文本编码检测的C语言库,其功能模块是用C++实现的,通过一定数量的字符样本独立的分析出文本的编码,当前已经支持UTF-8/GB13080/B ...

  6. day4学python 字符编码转换+元组概念

    字符编码转换+元组概念 字符编码转换 #coding:gbk //此处必声明 文件编码(看右下角编码格式) #用来得到python默认编码 import sys print(sys.getdefaul ...

  7. erlang中字符编码转换(转)

    转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来 ...

  8. 编码问题 php字符编码转换类

    各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...

  9. Python—字符编码转换、函数基本操作

    字符编码转换 函数 #声明文件编码,格式如下: #-*- coding:utf-8 -*- 注意此处只是声明了文件编码格式,python的默认编码还是unicode 字符编码转换: import sy ...

随机推荐

  1. Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。

    Python urllib模块urlopen()与urlretrieve()的使用方法详解   1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...

  2. linux 中ls命令函数

    #include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<dirent.h> ...

  3. 快速学习javascript对象-遍历对象

    为了方便了解每个javascript对象包含的方法,我写一个函数. function GetCollection(obj){ try{ if(obj){ var sType=""; ...

  4. PHP5的类与对象

    类是一个由变量和方法组成的独立程序块或功能模块. 人类: 口,脚,身高,体重,姓名,肤色等 (变量) 说话,行走等(方法) 丁三石,马化树,张夕阳(具体对象) 类的定义和对象的创建

  5. 解析Tensorflow官方English-Franch翻译器demo

    今天我们来解析下Tensorflow的Seq2Seq的demo.继上篇博客的PTM模型之后,Tensorflow官方也开放了名为translate的demo,这个demo对比之前的PTM要大了很多(首 ...

  6. Go语言AST尝试

    Go语言有很多工具, goimports用于package的自动导入或者删除, golint用于检查源码中不符合Go coding style的地方, 比如全名,注释等. 还有其它工具如gorenam ...

  7. linux的NetworkManager服务

    在开启NetworkManager服务的情况下,在终端下敲“service network restart”命令: 正在关闭接口 eth0: 设备状态:3 (断开连接) [确定] 正在关闭接口 eth ...

  8. KDE子项目一览 good

    https://www.kde.org/applications/development/ https://www.kde.org/applications/graphics/ https://www ...

  9. WIN ERROR:C:\Windows\System32\<LANG_NAME>\mstsc.exe.MUI

    Issue: When you upgrade Win7, you may found your remote desktop will not work. You may get following ...

  10. docker 运行挂载磁盘

    docker:/data# mkdir /awp docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...