我想将其他编码中的数据转换为UTF-8。我有以下问题:如何使用iconv进行utf8转换?
- 执行附加的代码给我:
pointer being freed was not allocated
in iconv()。 iconv为什么玩我的记忆? - 当我不释放(dst)它不会崩溃但没有打印。甚至没有胡言乱语。 有什么问题?
void utf8(char **dst, char **src, const char *enc)
{
iconv_t cd;
size_t len_src,
len_dst;
len_src = strlen(*src);
len_dst = len_src * 8; // is that enough for ASCII to UTF8?
cd = iconv_open("UTF-8", enc);
*dst = (char *)calloc(len_dst+1, 1);
iconv(cd, src, &len_src, dst, &len_dst);
iconv_close(cd);
}
int main(int argc, char **argv)
{
char *src = "hello world";
char *dst;
utf8(&dst, &src, "ASCII");
printf("%s\n", dst);
free(dst);
return 0;
}
FWIW,UTF-8字符每个永远不会超过6个字节。 'len_src * 8'是一个矫枉过正的问题。 – 2013-03-28 08:53:44