#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
//UTF8ToGB2132
string UTF8ToGB2132(string strSrc)
{
string result;
WCHAR *wstrSrc = NULL;
char *szRes = NULL;
int i;
// UTF8转换成Unicode
i = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);
wstrSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstrSrc, i);
// Unicode转换成GB2312
i = WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, NULL, 0, NULL, NULL);
szRes = new char[i+1];
WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, szRes, i, NULL, NULL);
result = string (szRes);
if (wstrSrc != NULL)
{
delete []wstrSrc;
wstrSrc = NULL;
}
if (szRes != NULL)
{
delete []szRes;
szRes = NULL;
}
return result;
}
//GB2132ToUTF8
string GB2132ToUTF8(string strSrc)
{
string result;
WCHAR *wstrSrc = NULL;
char *szRes = NULL;
int i;
// GB2312转换成Unicode
i = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, NULL, 0);
wstrSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, wstrSrc, i);
// Unicode转换成UTF8
i = WideCharToMultiByte(CP_UTF8, 0, wstrSrc, -1, NULL, 0, NULL, NULL);
szRes = new char[i+1];
WideCharToMultiByte(CP_UTF8, 0, wstrSrc, -1, szRes, i, NULL, NULL);
result = string (szRes);
if (wstrSrc != NULL)
{
delete []wstrSrc;
wstrSrc = NULL;
}
if (szRes != NULL)
{
delete []szRes;
szRes = NULL;
}
return result;
}
int main()
{
/*函数调用*/
retrun 0;
}
注:在VS2008下编译通过。