Converting CString to const char* in MFC Unicode Applications
In Unicode MFC applications, it may be necessary to convert a TCHAR CString to a const char*. This conversion allows for interoperability with other applications or components that utilize ASCII strings.
To facilitate this conversion, the CT2A macro can be employed. CT2A enables the conversion of the TCHAR CString to ASCII, as well as other Windows code pages, such as UTF8.
Example Code:
The following code demonstrates the use of CT2A to convert a TCHAR CString to ASCII, UTF8, and Thai code page:
<code class="cpp">// Convert using the local code page CString str(_T("Hello, world!")); CT2A ascii(str); TRACE(_T("ASCII: %S\n"), ascii.m_psz); // Convert to UTF8 CString str(_T("Some Unicode goodness")); CT2A ascii(str, CP_UTF8); TRACE(_T("UTF8: %S\n"), ascii.m_psz); // Convert to Thai code page CString str(_T("Some Thai text")); CT2A ascii(str, 874); TRACE(_T("Thai: %S\n"), ascii.m_psz);</code>
Additional Notes:
The above is the detailed content of How can I convert a CString to a const char* in MFC Unicode applications?. For more information, please follow other related articles on the PHP Chinese website!