LinuxÏÂת»»×Ö·û¼¯(UTF8ת»»)£¨×ª£© ÊÕ²Ø
ÔÚ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³ÌÐò */¾ßÌå½²£¬×Ô¼ºµÄÑé֤ʵÏÖÊǸù¾ÝÎÄÖеÄf.cʵÏÖµÄ
#include <iconv.h>
#define OUTLEN 255
main()
{
char *in_utf8 = "æ£å?¨å??è£?";<=======´Ë×Ö·û´®Ëƺõ²¢²»ÊÇ“ÕýÔÚ°²×°”Ëĸö×ÖµÄUTF8µÄ¶ÔÕÕ´®£¬
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,si