- 论坛徽章:
- 15
|
本帖最后由 yulihua49 于 2014-02-25 13:26 编辑
shan_ghost 发表于 2014-02-25 12:16 ![]()
求c里面把utf-8转成妹子的库函数 T.T
下面使用iconv库:
bool utf8togb2312Ykt(const char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
char *p=getenv("DB_USE_UTF8");
memset(destbuf,0,destlen);
if(p&&*p=='T') { //DB_USE_UTF8=True
if(g2u==NULL && (g2u = iconv_open("gb2312","utf-8")) ==0 )
return false;
if(-1 == iconv(g2u,(char **)&sourcebuf,&sourcelen,(char**)&destbuf,&destlen)) {
return false;
}
} else strcpy(destbuf,sourcebuf);
return true;
}
//将GB2312编码转换为utf-8编码
bool gb2312toutf8Ykt(const char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
char *p=getenv("DB_USE_UTF8");
memset(destbuf,0,destlen);
if(p&&*p=='T') { //DB_USE_UTF8=True
if(u2g==NULL && (u2g = iconv_open("utf-8","gb2312")) ==NULL )
return false;
if(-1 == iconv(u2g,(char**)&sourcebuf,&sourcelen,(char**)&destbuf,&destlen))
return false;
} else strcpy(destbuf,sourcebuf);
return true;
}
|
|