C++中宽字符和窄字符的相互转换

可以参考:

std::wstring ANSIToUniCode(const std::string &strCmd)

{

   int bytes =  ::MultiByteToWideChar(CP_ACP, 0, strCmd.c_str(), strCmd.size(), NULL,0);

   std::wstring wstrCmd;

   wstrCmd.resize(bytes);

   bytes  =  ::MultiByteToWideChar(CP_ACP, 0, strCmd.c_str(), strCmd.size(), const_cast<wchar_t*>(wstrCmd.c_str()), wstrCmd.size());

 

   return wstrCmd;

}

 

std::string UnicodeToANSI(const std::wstring &wstrCmd)

{

   int bytes =  ::WideCharToMultiByte(CP_ACP, 0, wstrCmd.c_str(), wstrCmd.size(), NULL,0, NULL, NULL);

   std::string strCmd;

   strCmd.resize(bytes);

   bytes  =  ::WideCharToMultiByte(CP_ACP, 0, wstrCmd.c_str(), wstrCmd.size(), const_cast<char*>(strCmd.data()), strCmd.size(), NULL, NULL);

 

   return strCmd;

}

 

std::string UnicodeToUTF8(const std::wstring &wstrCmd)

{

   int bytes =  ::WideCharToMultiByte(CP_UTF8, 0, wstrCmd.c_str(), wstrCmd.size(), NULL,0, NULL, NULL);

   std::string strCmd;

   strCmd.resize(bytes);

   bytes  =  ::WideCharToMultiByte(CP_UTF8, 0, wstrCmd.c_str(), wstrCmd.size(), const_cast<char*>(strCmd.data()), strCmd.size(), NULL, NULL);

 

   return strCmd;

}

你可能感兴趣的:(C++)