wstring和string的转化

std::wstring StringToWString(const std::string& s)
{
 std::wstring temp(s.length(),L' ');
 std::copy(s.begin(), s.end(), temp.begin());
 return temp;
}
std::string WStringToString(const std::wstring& s)
{
 std::string temp(s.length(), ' ');
 std::copy(s.begin(), s.end(), temp.begin());
 return temp;
}

 

///////////////////////////////////////////////////////////////////////
std::wstring CSOAP::StringToWString(const std::string& s)
{
 //std::wstring temp(s.length(),L' ');
 //std::copy(s.begin(), s.end(), temp.begin());
 //return temp;
 int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)s.c_str(), s.length(), 0, 0);
 if(nSize <= 0) return NULL;
 WCHAR *pwszDst = new WCHAR[nSize+1];
 if( NULL == pwszDst) return NULL;
 MultiByteToWideChar(CP_ACP, 0,(LPCSTR)s.c_str(), s.length(), pwszDst, nSize);
 pwszDst[nSize] = 0;
 if( pwszDst[0] == 0xFEFF) // skip Oxfeff
  for(int i = 0; i < nSize; i ++)
   pwszDst[i] = pwszDst[i+1];
 wstring wcharString(pwszDst);
 delete pwszDst;
 return wcharString;


}
std::string CSOAP::WStringToString(const std::wstring& s)
{
 //std::string temp(s.length(), ' ');
 //std::copy(s.begin(), s.end(), temp.begin());
 //return temp;
 int nLen = WideCharToMultiByte(CP_ACP, 0, s.c_str(), -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, s.c_str(), -1, pszDst, nLen, NULL, NULL);
 pszDst[nLen -1] = 0;
 std::string strTemp(pszDst);
 delete [] pszDst;
 return strTemp;


}

posted @ 2012-12-06 15:27  废弃账号  阅读(233)  评论(0编辑  收藏  举报