Convert CString to const char*(将 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
"), ascii.m_psz);
// Convert to UTF8
CString str(_T("Some Unicode goodness"));
CT2A ascii(str, CP_UTF8);
TRACE(_T("UTF8: %S
"), ascii.m_psz);
// Convert to Thai code page
CString str(_T("Some Thai text"));
CT2A ascii(str, 874);
TRACE(_T("Thai: %S
"), 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.
See the MSDN for more info.
这篇关于将 CString 转换为 const char*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 CString 转换为 const char*
基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
