How to convert between 'CString' and 'std::string' ?


When you are developing MFC project in most cases it becomes necessary to convert between 'CString' and 'std::string' .


'CString' to 'std::string':

 CString sTest ("Matrix");

  // Convert a TCHAR string to a LPCSTR
 CT2CA CStringToAscii(sTest);

  // construct a std::string using the LPCSTR input
 std::string sResultedString (CStringToAscii);

'std::string' to 'CString':

std::string sTest("Matrix");
CString ResultedCString(sTest.c_str());

Post a Comment