Home > Backend Development > C++ > body text

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

Mary-Kate Olsen
Release: 2024-10-28 22:23:02
Original
853 people have browsed it

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

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

Additional Notes:

  • The CA2T macro can also be used to convert from ASCII to Unicode.
  • These macros can be utilized in ATL/WTL applications with Visual Studio 2003 or later.
  • For further information, refer to the Microsoft Developer Network (MSDN) documentation.

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!

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!