編碼格式的轉換UTF8<——>GBK

GBK兼容GB2312,同時在GB2312標準的基礎上擴展了GB13000包含的字。ide

CComVar CComVar::ToGBK(const char * strUtf8 ) 
{
	int len=MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL,0);
	LPWSTR wszGBK = new WCHAR[len+1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
	CComVar mVar(wszGBK);
	delete[] wszGBK;
	return mVar;
}

std::string CComVar::ToUTF8() const
{
	std::string strTemp = ToString();
	int len=MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, NULL,0);
	//unsigned short * wszUtf8 = new unsigned short[len+1];
	WCHAR* wszUtf8 = new WCHAR[len+1];  
	memset(wszUtf8, 0, len * 2 + 2);
	MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, wszUtf8, len);

	len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
	char *szUtf8=new char[len + 1];
	memset(szUtf8, 0, len + 1);
	WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);

	std::string mVar(szUtf8);
	delete[] szUtf8;
	delete[] wszUtf8;

	return mVar;
}
相關文章
相關標籤/搜索