标题
[iconv/libxml/utf8/gbk/gb2312]libxml2 需要的 utf-8 转换代码
clq
浏览(0) +
2008-04-28 21:22:11 发表
编辑
关键字:
[2022-04-19 13:06:02 最后更新]
[iconv/libxml/utf8/gbk/gb2312]libxml2 需要的 utf-8 转换代码
xmlNodeListGetString() 的结果是 utf8 的要用 iconv 转换成 gb2312 编码。
//代码转换:从一种编码转为另一种编码
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, (const char **)pin, (unsigned int *)&inlen, pout, (unsigned int *)&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,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
//要从 utf8 转换成 gbk
std::string CVXml::ToGbk(xmlChar * utf8)
{
std::string r = "";
/*
char * encoding = "ISO-8859-1";//"gb2312";//"GBK";
xmlCharEncodingHandlerPtr handler = NULL;
int size = strlen((const char *)utf8)+1;
int out_size = (size * 2) -1;
char * out = (char *)malloc((size_t)out_size);
handler = xmlFindCharEncodingHandler(encoding);
int temp = 0;
handler->input((unsigned char *)out, &out_size, utf8, &temp);
//xmlSaveFormatFileEnc("-", doc, encoding, 1);
r = out;
*/
char * in_utf8 = (char *)utf8;//"姝e?ㄥ??瑁?";
char * in_gb2312 = "正在安装";
tmem tmp(strlen((const char *)utf8)*4 + 1);
//unicode码转为gb2312码
int rc = u2g(in_utf8,strlen(in_utf8), tmp.buf, tmp.buf_len);
//printf("unicode-->gb2312 out=%sn",out);
//gb2312码转为unicode码
//rc = g2u(in_gb2312,strlen(in_gb2312),tmp.buf, tmp.buf_len);
r = tmp.buf;
return r;
}
NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.