我想將其他編碼中的數據轉換爲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