直接上代码
bool gbk2utf8(const char* src, char* dest, size_t inlen) { const char *inbuf = src; size_t outlen = inlen * 4; char *outbuf = dest; memset(outbuf, 0, outlen); iconv_t cd = iconv_open("UTF-8", "GBK"); /* iconv接口会改变inbuf和outbuf,所以不能直接使用src和dest */ size_t res = iconv(cd, &inbuf, &inlen, &outbuf, &outlen); iconv_close(cd); return (0 == res) ? true : false; }
示例:
boost::shared_array<char> ptr_iType(new char[ilen * 4]); gbk2utf8(XML_ITYPE_DESC, ptr_iType.get(), ilen);
此处使用了boost的智能指针,减少内存泄漏的可能。
时间: 2024-11-10 07:40:38