#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iconv.h>
#include <errno.h>
#include <string.h>
iconv_t charset_init(char *tocharset,char *fromcharset)
{
iconv_t charst;
if(NULL == fromcharset || NULL == tocharset ||
0 == strlen(fromcharset) || 0 == strlen(tocharset))
return NULL;
charst = iconv_open(tocharset, fromcharset);
if((iconv_t)-1 == charst)
{
printf("iconv open error %s\n",strerror(errno));
return (iconv_t)-1;
}
return charst;
}
void charset_close(iconv_t charst)
{
if(charst < 0)
return;
iconv_close(charst);
return;
}
void change_charset(iconv_t charst,char *inbuf,size_t *inlen,char *outbuf,size_t *olen)
{
size_t re;
re = iconv(charst, &inbuf,inlen,&outbuf,olen);
if(-1 == re)
{
printf("iconv error %s\n",strerror(errno));
return;
}
return;
}
/*
使用以下方式将缓存中字符串换成另一种编码字符输出
charst = charset_init(tocharset,fromcharset);
if(-1 == charst)
...
change_charset(charst, inbuf,&inlen,outbuf,&outlen);
charset_close(charst);
*/