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;}int main() { char out[400]; char str_str[400]; unsigned short test[400]; wchar_t wcode[400]; unsigned int inlen; unsigned int outlen=200; int result; string str = "汉"; memset(str_str,0,sizeof(str_str)); memcpy(str_str,str.c_str(),strlen(str.c_str())); result=code_convert("UTF8","GB18030",str_str,inlen,out,OUTLEN); printf("result = %dn",result); } |
我照着网上的范例写了这个程序,想把GB18030的中文字符集转换成UTF-8的字符集,但是一直都不成功,每次执行的result结果都是-1,大家帮忙看看是什么原因