Compilation: Gcc-o F.exe F.c-liconv
#include <stdio.h>#include<stdlib.h>#include<stddef.h>#include<string.h>#include<unistd.h>#include<iconv.h>#defineOutlen 255Main () {Char*in_utf8 ="Shu E?ㄥ?? Turtle?"; Char*in_gb2312 ="who are you ?"; Char out[Outlen]; intRC; //Unicode code converted to gb2312 coderc = u2g (In_utf8,strlen (In_utf8), out, Outlen); //printf ("unicode-->gb2312 out=%sn", out); //gb2312 code converted to Unicode coderc = g2u (In_gb2312,strlen (in_gb2312), out, Outlen); printf ("Gb2312-->unicode OUT=%SN", out);}/*transcoding: Converting from one encoding to another*/intCode_convert (Char*from_charset,Char*to_charset,Char*inbuf,intInlen,Char*outbuf,intOutlen) {iconv_t cd; intRC; 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 code converted to GB2312 code*/intU2G (Char*inbuf,intInlen,Char*outbuf,intOutlen) { returnCode_convert ("Utf-8","gb2312", Inbuf,inlen,outbuf,outlen);}/*GB2312 code converted to Unicode code*/intG2U (Char*inbuf,size_t Inlen,Char*outbuf,size_t Outlen) { returnCode_convert ("gb2312","Utf-8", Inbuf,inlen,outbuf,outlen);}
C language Encoding conversion gb2312 to Utf8,utf8 to gb2312 code, GCC compilation, support for Windows, Linux