gpt4 book ai didi

c++ - 将 CString 转换为 const char*

转载 作者:IT老高 更新时间:2023-10-28 13:57:54 25 4
gpt4 key购买 nike

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

最佳答案

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

// 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 应用程序中使用这些宏。

MSDN了解更多信息。

关于c++ - 将 CString 转换为 const char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/859304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com