Linux字符编码转换 UTF8转GB3212

在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);

时间: 2024-08-14 17:27:03

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

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

Linux字符编码默认为UTF-8,如出现乱码可设置为GBK1.手动更改profile文件的命令: vi /etc/profile 也可以修改 /etc/sysconfig/i18n 文件,如 LANG="en_US.UTF-8" LANG="zh_CN.GB18030" 还有一种方法 cp /etc/sysconfig/i18n $HOME/.i18n 修改 $HOME/.i18n 文件,如 LANG="en_US.UTF-8" LANG=&q

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

由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 >mysql -uroot -p #输入密码进入 >status; #查看当前MySQL运行状态,如下图所示: 2.修改mysql配置文件 [[email protected] ~]# vi /etc/my.cnf #在[client]段增加下面代码 default-character-set=utf

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

在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ( string in_charset, string out_charset, string str ) 注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符

字符编码转换笔记

何为字符编码? 字符编码为计算机文字的存储格式, 例如 英文 字母 以ASCII编码存储, 即单字节存储,  其他字符编码有 UTF-8(通用字符编码格式), 其他区域性编码格式, 例如 ISO-8859(西欧), windows-1251俄文,中文GB编码. 为什么需要转换? 正因各个地区有不同的编码格式, 为了交换信息的目的, 就需要将相同字符的 从一种编码格式 转换为 另外一种编码格式. 通用的编码格式为 UTF-8, 其囊括了 世界上所有字符, 所以一般为了通用性, 文件都以UTF-8编

Windows下字符编码转换

有时候经常使用别人用Tabhost+其它的实现demo.单纯利用Tabhost该如何使用呢? 下面看例子: public class MainActivity extends TabActivity { public TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取对象 tabHost = getTabH

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

在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时,可以稍微弥补一下默认缺陷,不至于无法转换是截断,用法如下 iconv(“UTF-8″,”GB2312//IGNORE”,$data) ; 这样碰到生僻字转换失败时,它就会忽略失败,继续转换下面的内容,这算解决问题的一个办法,不过为了确保转换的成功率,我们可以用另一个转换函数(mb_convert_e

iconv字符编码转换

转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/libiconv/)是一个开源的字符编码转换库,可以"方便"的完成几乎所有的编码转换工作.说简单是因为,它常用的接口就三个,iconv_open  iconv   iconv_close,但是即便是只有三个接口,要想使用正确也不容易.这里把一些基本概念和使用细节记录下来,希望能成为一篇最实用的

ASP中有关字符编码转换的几个有用函数

ASP中有关字符编码转换的几个有用函数 <%1.'UTF转GB---将UTF8编码文字转换为GB编码文字function UTF2GB(UTFStr) for Dig=1 to len(UTFStr)   '如果UTF8编码文字以%开头则进行转换  if mid(UTFStr,Dig,1)="%" then      'UTF8编码文字大于8则转换为汉字    if len(UTFStr) >= Dig+8 then        GBStr=GBStr & Con

python基础 字符编码转换

python2 1 #python2上所有的字符编码都需要先decode到unicode,再从unicode encode到目标编码 2 str_utf8 = "我就是我" 3 print("str_utf-8:我就是我:",str_utf8) 4 #将utf-8转换为unicode 5 str_utf8_to_unicode = str_utf8.decode("utf-8") 6 print(str_utf8_to_unicode) 7 #将