C++ 将 CString 转换为 const char*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/859304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:36:51  来源:igfitidea点击:

Convert CString to const char*

c++visual-studiovisual-c++unicodemfc

提问by Attilah

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

如何在我的 Unicode MFC 应用程序中转换CStringconst char*

回答by Rob

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

要将TCHARCString转换为 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);

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.

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

See the MSDNfor more info.

有关更多信息,请参阅MSDN

回答by Mark Ransom

If your CString is Unicode, you'll need to do a conversion to multi-byte characters. Fortunately there is a version of CString which will do this automatically.

如果您的 CString 是 Unicode,您将需要转换为多字节字符。幸运的是,有一个 CString 版本可以自动执行此操作。

CString unicodestr = _T("Testing");
CStringA charstr(unicodestr);
DoMyStuff((const char *) charstr);

回答by RichieHindle

Note: This answer predates the Unicode requirement; see the comments.

注意:此答案早于 Unicode 要求;见评论。

Just cast it:

只需投射它:

CString s;
const TCHAR* x = (LPCTSTR) s;

It works because CString has a cast operator to do exactly this.

它之所以有效,是因为 CString 有一个强制转换运算符可以做到这一点。

Using TCHAR makes your code Unicode-independent; if you're not concerned about Unicode you can simply use charinstead of TCHAR.

使用 TCHAR 使您的代码独立于 Unicode;如果你不关心Unicode的你可以简单地使用char代替TCHAR

回答by Reed Copsey

There is an explicit cast on CString to LPCTSTR, so you can do (provided unicode is not specified):

有一个 CString 到 LPCTSTR 的显式转换,所以你可以这样做(如果未指定 unicode):

CString str;
// ....
const char* cstr = (LPCTSTR)str;

回答by Mr.What

I had a similar problem. I had a char*buffer with the .so name in it.
I could not convert the char*variable to LPCTSTR. Here's how I got around it...

我有一个类似的问题。我有一个char*带有 .so 名称的缓冲区。
我无法将char*变量转换为LPCTSTR. 这就是我解决它的方法...

char *fNam;
...
LPCSTR nam = fNam;
dll = LoadLibraryA(nam);

回答by felixjimcal

I recommendo to you use TtoC from ConvUnicode.h

我建议你使用 ConvUnicode.h 中的 TtoC

const CString word= "hello";
const char* myFile = TtoC(path.GetString());

It is a macro to do conversions per Unicode

这是一个按 Unicode 进行转换的宏

回答by Amit G.

Generic Conversion Macros (TN059 Other Considerations section is important):

通用转换宏(TN059 其他注意事项部分很重要):

A2CW     (LPCSTR)  -> (LPCWSTR)  
A2W      (LPCSTR)  -> (LPWSTR)  
W2CA     (LPCWSTR) -> (LPCSTR)  
W2A      (LPCWSTR) -> (LPSTR)