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*


基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++,'if' 表达式中的变量声明 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01