将宽 CString 转换为 char*

Convert wide CString to char*

这个问题被问过很多次,答案也很多次 - none 其中对我有用,而且似乎对其他许多人也有用。问题是关于 MFC 下的宽 CString 和 8 位字符。我们都想要一个适用于所有情况的答案,而不是特定实例。

void Dosomething(CString csFileName)
{
    char cLocFileNamestr[1024];
    char cIntFileNamestr[1024];
    // Convert from whatever version of CString is supplied
    // to an 8 bit char string
    cIntFileNamestr = ConvertCStochar(csFileName);

    sprintf_s(cLocFileNamestr, "%s_%s", cIntFileNamestr, "pling.txt" );
    m_KFile = fopen(LocFileNamestr, "wt");
}

这是对现有代码(由其他人编写)的补充,用于调试。 我不想更改函数签名,它在很多地方都被使用。 我无法更改 sprintf_s 的签名,它是一个库函数。

您漏掉或忽略了很多细节。如果您使用定义的 UNICODE 进行构建(看起来您是这样),那么转换为 MBCS 的最简单方法如下:

CStringA strAIntFileNameStr = csFileName.GetString(); // uses default code page

CStringA 是 CString 的 8-bit/MBCS 版本。

但是,如果您要翻译的 unicode 字符串包含默认代码页中没有的字符,它将填充一些垃圾字符。

您可以使用 _wfopen() 而不是使用 fopen(),这将打开一个具有 unicode 文件名的文件。要创建文件名,您可以使用 swprintf_s().

an answer that will work in ALL cases, not a specific instance...

没有这样的事情。

"ABCD..."wchar_t* 转换为 char* 很容易,但它不适用于非拉丁语言.

如果您的项目是 unicode,请坚持使用 CStringwchar_t

如果需要上传数据到网页什么的,就用CW2ACA2W进行utf-8和utf-16的转换。

CStringW unicode = L"Россия";
MessageBoxW(0,unicode,L"Russian",0);//should be okay

CStringA utf8 = CW2A(unicode, CP_UTF8);
::MessageBoxA(0,utf8,"format error",0);//WinApi doesn't get UTF-8

char buf[1024];
strcpy(buf, utf8);
::MessageBoxA(0,buf,"format error",0);//same problem

//send this buf to webpage or other utf-8 systems
//this should be compatible with notepad etc. 
//text will appear correctly
ofstream f(L"c:\stuff\okay.txt");
f.write(buf, strlen(buf));

//convert utf8 back to utf16
unicode = CA2W(buf, CP_UTF8);
::MessageBoxW(0,unicode,L"okay",0);