Home  >  Article  >  Backend Development  >  How do I convert a CString to const char* in Unicode MFC applications?

How do I convert a CString to const char* in Unicode MFC applications?

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 10:23:30384browse

How do I convert a CString to const char* in Unicode MFC applications?

Converting CString to const char* in Unicode MFC Applications

Converting a TCHAR CString to ASCII in a Unicode MFC application requires the usage of the CT2A macro. This macro allows for the conversion of the string to various encodings, including ASCII, UTF8, and others.

Code Example:

To convert a CString to ASCII using the local code page:

<code class="cpp">CString str(_T("Hello, world!"));
CT2A ascii(str);
TRACE(_T("ASCII: %S\n"), ascii.m_psz);</code>

To convert to UTF8:

<code class="cpp">CString str(_T("Some Unicode goodness"));
CT2A ascii(str, CP_UTF8);
TRACE(_T("UTF8: %S\n"), ascii.m_psz);</code>

To convert to a specific code page, such as Thai (874):

<code class="cpp">CString str(_T("Some Thai text"));
CT2A ascii(str, 874);
TRACE(_T("Thai: %S\n"), ascii.m_psz);</code>

Additionally, there is a macro called CA2T for converting from ASCII to Unicode, which can be used in ATL/WTL applications with Visual Studio 2003 or later.

For further details, consult the MSDN documentation on these macros.

The above is the detailed content of How do I convert a CString to const char* in Unicode MFC applications?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn