linux下的转码是通过 iconv 模块实现的
废话少说,直接上源码
#include <stdio.h>
#include <iconv.h>
#include <string.h>
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,(size_t *)&inlen,pout,(size_t*)&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
void main(int argc,char ** argv)
{
char * utf8Char = "嘎嘎哈哈哈哈哈哈"; //请在utf8下编辑此文件
printf("%s",utf8Char);
char gb2312Char[255] = "";
code_convert("gb2312","utf-8",utf8Char,strlen(utf8Char),gb2312Char,255);
printf("%s",gb2312Char);
}