Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://www.codeguru.com/forum/archive/index.php/t-231155.html" rel="noreferrer">CodeGuru</a>:</p> <p><code>CString</code> to <code>std::string</code>:</p> <pre><code>CString cs("Hello"); std::string s((LPCTSTR)cs); </code></pre> <p><strong>BUT:</strong> <code>std::string</code> cannot always construct from a <code>LPCTSTR</code>. i.e. the code will fail for UNICODE builds.</p> <p>As <code>std::string</code> can construct only from <code>LPSTR</code> / <code>LPCSTR</code>, a programmer who uses VC++ 7.x or better can utilize conversion classes such as <code>CT2CA</code> as an intermediary.</p> <pre><code>CString cs ("Hello"); // Convert a TCHAR string to a LPCSTR CT2CA pszConvertedAnsiString (cs); // construct a std::string using the LPCSTR input std::string strStd (pszConvertedAnsiString); </code></pre> <p><a href="http://cstrings.blogspot.com/2006/08/how-to-convert-from-stdstring-to.html" rel="noreferrer"><code>std::string</code> to <code>CString</code></a>: (From <a href="http://cstrings.blogspot.com/2006/08/how-to-convert-from-stdstring-to.html" rel="noreferrer">Visual Studio's CString FAQs...</a>)</p> <pre><code>std::string s("Hello"); CString cs(s.c_str()); </code></pre> <p><code>CStringT</code> can construct from both character or wide-character strings. i.e. It can convert from <code>char*</code> (i.e. <code>LPSTR</code>) or from <code>wchar_t*</code> (<code>LPWSTR</code>).</p> <p>In other words, char-specialization (of <code>CStringT</code>) i.e. <code>CStringA</code>, <code>wchar_t</code>-specilization <code>CStringW</code>, and <code>TCHAR</code>-specialization <code>CString</code> can be constructed from either <code>char</code> or wide-character, <del>null terminated (null-termination is very important here)</del> string sources.<br> Althoug <a href="https://stackoverflow.com/users/1889329/iinspectable">IInspectable</a> amends the "null-termination" part <a href="https://stackoverflow.com/questions/258050/how-to-convert-cstring-and-stdstring-stdwstring-to-each-other/258052?noredirect=1#comment29168168_258052">in the comments</a>:</p> <blockquote> <p><strong>NUL-termination is not required</strong>.<br> <code>CStringT</code> has conversion constructors that take an explicit length argument. This also means that you can construct <code>CStringT</code> objects from <code>std::string</code> objects with embedded <code>NUL</code> characters.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload