编码格式的转换UTF8<——>GBK

GBK兼容GB2312,同时在GB2312标准的基础上扩展了GB13000包含的字。

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;
}
本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院