#include <string>
using namespace std;
/************************************************************************/
/* 将gb2312编码转为UTF-8字符 */
/************************************************************************/
std::string GBToUTF8(const char*);
/************************************************************************/
/* 将UTF-8编码转为gb2312字符 */
/************************************************************************/
std::string UTF8ToGB(const char*);
std::string GBToUTF8(const char* str)
{
std::string result;
WCHAR *strSrc;
//TCHAR *szRes;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);
//获得临时变量的大小
//i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
//szRes = new TCHAR[i+1];
//int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szSrc;
delete []strSrc;
//delete []szRes;
return result;
}
std::string UTF8ToGB(const char* str)
{
std::string result;
WCHAR *strSrc;
//TCHAR *szRes;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
//i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
//szRes = new TCHAR[i+1];
//WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szSrc;
delete []strSrc;
//delete []szRes;
return result;
}
忘记在哪拿来的了,以后找到了再写原帖,如果有发现的,可以告知,谢谢!