UTF-8、GBK等格式互相转换 [Windows/Linux 通用]
#ifdef WIN32
#include <windows.h>
#endif
#ifdef OS_UNIX
#include <iconv.h>
#endif
#if defined(WIN32)
void convert_encoding(const char *input, int from_codepage, int to_codepage, char *output)
{
int wlen = MultiByteToWideChar(from_codepage, 0, input, -1, NULL, 0);
if (0 == wlen)
return;
wchar_t *wbuf = (wchar_t *)malloc((wlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(from_codepage, 0, input, -1, wbuf, wlen);
int len = WideCharToMultiByte(to_codepage, 0, wbuf, -1, NULL, 0, NULL, NULL);
if (0 == len)
{
free(wbuf);
return;
}
WideCharToMultiByte(to_codepage, 0, wbuf, -1, output, len, NULL, NULL);
free(wbuf);
}
#else
void convert_encoding(const char *input, const char *from_encoding, const char *to_encoding, char *output)
{
iconv_t cd = iconv_open(to_encoding, from_encoding);
if (cd == (i