Home > Backend Development > C++ > body text

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

Barbara Streisand
Release: 2024-10-26 10:23:30
Original
383 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!