wchar_t*转string,wstring转string

// wchar_t* -> string
std::string wchar2string( const wchar_t* pwchar )
{
	int nLen = WideCharToMultiByte(CP_ACP, 0, pwchar, -1, NULL, 0, NULL, NULL);
	if (nLen<= 0) return std::string("");
	char* pszDst = new char[nLen];
	if (NULL == pszDst) return std::string("");
	WideCharToMultiByte(CP_ACP, 0, pwchar, -1, pszDst, nLen, NULL, NULL);
	pszDst[nLen -1] = 0;
	std::string strTemp(pszDst);
	delete [] pszDst;
	return strTemp;
}

// wstring -> string
std::string wstring2string( const std::wstring & wstr )
{
	return wchar2string(wstr.c_str());
}


 

你可能感兴趣的:(wchar_t*转string,wstring转string)