2025年6月8日 星期日 乙巳(蛇)年 三月十二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

wstring与string互转

时间:01-18来源:作者:点击数:45

wstring与string互转

  • std::wstring StringToWString(const std::string &strSrc)
  • {
  • int nLen = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strSrc.c_str(), strSrc.length(), 0, 0);
  • if (nLen <= 0) return std::wstring(L"");
  • WCHAR *pwszDst = new WCHAR[nLen + 1];
  • if (NULL == pwszDst) return std::wstring(L"");
  • memset(pwszDst, 0, nLen + 1);
  • MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strSrc.c_str(), strSrc.length(), pwszDst, nLen);
  • pwszDst[nLen] = 0;
  • std::wstring wstrTemp(pwszDst);
  • delete pwszDst;
  • pwszDst = NULL;
  • return wstrTemp;
  • }
  • std::string WStringToString(const std::wstring &wstrSrc)
  • {
  • int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstrSrc.c_str(), -1, NULL, 0, NULL, NULL);
  • if (nLen <= 0) return std::string("");
  • char* pszDst = new char[nLen + 1];
  • if (NULL == pszDst) return std::string("");
  • memset(pszDst, 0, nLen + 1);
  • WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstrSrc.c_str(), -1, pszDst, nLen, NULL, NULL);
  • pszDst[nLen] = 0;
  • std::string strTemp(pszDst);
  • delete[] pszDst;
  • pszDst = NULL;
  • return strTemp;
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门