1. 程式人生 > >字元編碼格式轉換

字元編碼格式轉換

int gb2312toutf8(const char *sourcebuf, size_t sourcelen, char *destbuf, size_t destlen)
{
        iconv_t ict;
        ict = iconv_open("utf-8", "gb2312");
        memset(destbuf, 0, destlen);
        const char **source = &sourcebuf;
        char **dest = &destbuf;
        iconv(ict, (char**)(source), &sourcelen, dest, &destlen);
        iconv_close(ict);
        return 0;
}

一般情況下,一個utf-8字元佔用3個字元空間,一個gb2312是2個字元空間,轉換之前需要申請足夠空間存放相應的字元