将CString转换为const char * [英] Convert CString to const char*

查看:1060
本文介绍了将CString转换为const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的Unicode MFC应用程序中将 CString 转换为 const char *

How do I convert from CString to const char* in my Unicode MFC application?

推荐答案

要将 TCHAR CString转换为ASCII,请使用 CT2A 宏 - 这也允许您将字符串转换为UTF8(或任何其他Windows代码页):

To convert a TCHAR CString to ASCII, use the CT2A macro - this will also allow you to convert the string to UTF8 (or any other Windows code page):

// 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);

还有一个宏可以从ASCII - > Unicode( CA2T ),只要您有VS2003或更高版本,您就可以在ATL / WTL应用程序中使用。

There is also a macro to convert from ASCII -> Unicode (CA2T) and you can use these in ATL/WTL apps as long as you have VS2003 or greater.

请参阅MSDN 了解详情。

这篇关于将CString转换为const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆