在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码。 
   一、利用iconv函数族进行编码转换 
   iconv函数族的头文
在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码。
   一、利用iconv函数族进行编码转换
   iconv函数族的头文件是iconv.h,使用前需包含之。
   #include <iconv.h>
   iconv函数族有三个函数,原型如下:
   (1) iconv_t iconv_open(const char *tocode, const char *fromcode);
   此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。
   (2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
   此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。 (3) int iconv_close(iconv_t cd);
   此函数用于关闭转换句柄,释放资源。
   例子1: 用C语言实现的转换示例程序

/* f.c : 代码转换示例C程序 */
   #include <iconv.h>
   #define OUTLEN 255
   main()
   {
   char *in_utf8 = "姝e?ㄥ??瑁?";
   char *in_gb2312 = "正在安装";
   char out[OUTLEN];

//unicode码转为gb2312码
   rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
   printf("unicode-->gb2312 out=%sn",out);
   //gb2312码转为unicode码
   rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
   printf("gb2312-->unicode out=%sn",out);
   }
   //代码转换:从一种编码转为另一种编码
   int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
   {
   iconv_t cd;
   int rc;
   char **pin = &inbuf;
   char **pout = &outbuf;

cd = iconv_open(to_charset,from_charset);
   if (cd==0) return -1;
   memset(outbuf,0,outlen);
   if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
   iconv_close(cd);
   return 0;
   }
   //UNICODE码转为GB2312码
   int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
   {
   return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
   }
   //GB2312码转为UNICODE码
   int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
   {
   return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
   }

例子2: 用C++语言实现的转换示例程序

/* f.cpp : 代码转换示例C++程序 */
   #include <iconv.h>
   #include <iostream>

#define OUTLEN 255

using namespace std;

// 代码转换操作类
   class CodeConverter {
   private:
   iconv_t cd;
   public:
   // 构造
   CodeConverter(const char *from_charset,const char *to_charset) {
   cd = iconv_open(to_charset,from_charset);
   }

// 析构
   ~CodeConverter() {
   iconv_close(cd);
   }

// 转换输出
   int convert(char *inbuf,int inlen,char *outbuf,int outlen) {
   char **pin = &inbuf;
   char **pout = &outbuf;

memset(outbuf,0,outlen);
   return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen);
   }
   };

int main(int argc, char **argv)
   {
   char *in_utf8 = "姝e?ㄥ??瑁?";
   char *in_gb2312 = "正在安装";
   char out[OUTLEN];

// utf-8-->gb2312
   CodeConverter cc = CodeConverter("utf-8","gb2312");
   cc.convert(in_utf8,strlen(in_utf8),out,OUTLEN);
   cout << "utf-8-->gb2312 in=" << in_utf8 << ",out=" << out << endl;

// gb2312-->utf-8
   CodeConverter cc2 = CodeConverter("gb2312","utf-8");
   cc2.convert(in_gb2312,strlen(in_gb2312),out,OUTLEN);
   cout << "gb2312-->utf-8 in=" << in_gb2312 << ",out=" << out << endl;
   }

 
linux C 字符集转换,UTF-8,GB2312
最近帮朋友写个系统接口的小东东,2个系统字符集不同,一个采用UTF-8,一个采用GB2312,不得已需要转换字符集。转换函数记录如下:

#include <iconv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OUTLEN 255
main()
{
char *in_utf8 = "utf8字符串";
char *in_gb2312 = "\xbe\xb2\xcc\xac\xc4\xa3\xca\xbd";

char out[OUTLEN];
int rec ;

//unicode码转为gb2312码
rec = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
printf("unicode-->gb2312 out=%s\n",out);
  
//gb2312码转为unicode码
rec = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
printf("gb2312-->unicode out=%s \n",out);
}
//代码转换:从一种编码转为另一种编码
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;

cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
//UNICODE码转为GB2312码
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
//GB2312码转为UNICODE码
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);

Linux字符编码转换 UTF8转GB3212的更多相关文章

  1. linux字符编码防止乱码

    一:linux字符编码 en_US.UTF-8 : 美式英文,utf-8 zh_CN.UTF-8 临时优化 export LANG=zh_CN.UTF-8 : 设置编码 永久优化 vim /etc/l ...

  2. Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码

    由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...

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

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

  4. php转换字符编码为utf-8

    php转换字符编码为utf-8 function strToUtf8($str){ $encode = mb_detect_encoding($str, array("ASCII" ...

  5. Linux字符编码默认为UTF-8,如出现乱码可设置为GBK

    Linux字符编码默认为UTF-8,如出现乱码可设置为GBK1.手动更改profile文件的命令: vi /etc/profile 也可以修改 /etc/sysconfig/i18n 文件,如 LAN ...

  6. iconv字符编码转换

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

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

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

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

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

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

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

随机推荐

  1. VBS调用keybd_event事件

    ----------------发送alt+v组合按键----------------------Set Wrap = CreateObject("DynamicWrapper") ...

  2. 认识ASP.NET MVC的5种AuthorizationFilter

    在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...

  3. C#在Json反序列化中处理键的特殊字符

    假设有如下Json 数据: 1.{ 2."id" : 1, 3."@value" : "this a @", 4."$p" ...

  4. JS调用OC方法

    - (void)myMethod:(CDVInvokedUrlCommand*)command { NSString* echo = [command.arguments objectAtIndex: ...

  5. sql server字段是逗号分割的id,关联明细表查询

    有时候一张表的一个字段是以逗号分割的一个字符串,分割的数字是明细表的主键id. 关联明细表查询可以这样做: ) ) --这是把areanos字段赋值给@areanos变量 set @areanos=' ...

  6. 【Tomcat】Tomcat配置之请求字符串编码

    默认情况下,如果tomcat中部署的webservice或者web网站需要有中文的请求参数,而这时候我们直接在浏览器中输入中文那么接受到的将是乱码,无法达到我们的需求,这时候我们就需要对Tomcat的 ...

  7. 【sort】 基数排序

    下面这段问答摘自csdn: 把基数排序说成桶排序应该是没有太大问题的.总的说来,应该把这一类归为分配排序,由于分配排序的一些缺陷,主要是时间代价很差,改进成为桶式排序(bucket sort),而桶排 ...

  8. HDU5907 Find Q 数学

    题目大意:求当前串中只含q的连续子串的个数 题目思路:水题,但要注意的是计算过程中可能超int范围; #include<iostream> #include<algorithm> ...

  9. Python作用域

    以下依据Python 3 1.Python变量查找顺序为LEGB(L:Local,E:Enclosing,G:Global,B:Built-in). 2.实际上,在Python中,只有模块,类以及函数 ...

  10. php 大转盘抽奖

    包在文件中 lottery.zip <!DOCTYPE HTML><html><head><meta charset="utf-8"> ...