How to Convert a CString to a const char* in Unicode MFC Applications?
Oct 27, 2024 pm 07:30 PMConverting 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>
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
