Home > Backend Development > C++ > body text

How to Convert a CString to a const char* in Unicode MFC Applications?

Patricia Arquette
Release: 2024-10-27 19:30:30
Original
496 people have browsed it

How to Convert a CString to a const char* in Unicode MFC Applications?

Converting CString to Const char* in Unicode MFC Applications

Problem:

How can I transform a CString, particularly when it contains TCHAR characters, to a const char* representation in a Unicode MFC application?

Solution:

To effectively convert a TCHAR CString to ASCII, you should utilize the CT2A macro. This macro not only allows for ASCII conversion but also enables conversion to UTF8 or other Windows code pages. Here are some examples to demonstrate its usage:

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

In addition, there is a macro for converting from ASCII to Unicode (CA2T). You can utilize these macros in ATL/WTL applications provided you have VS2003 or later. More detailed information can be found in the MSDN documentation.

The above is the detailed content of How to Convert a CString to a 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!