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

来源:互联网 发布:机器手编程语言 编辑:程序博客网 时间:2024/05/16 13:44

可以参考:

std::wstringANSIToUniCode(conststd::string &strCmd)

{

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

   std::wstringwstrCmd;

   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::stringUnicodeToANSI(conststd::wstring &wstrCmd)

{

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

   std::stringstrCmd;

   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::stringUnicodeToUTF8(conststd::wstring &wstrCmd)

{

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

   std::stringstrCmd;

   strCmd.resize(bytes);

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

 

   return strCmd;

}

原创粉丝点击