如何将tchar指针转换为char指针

时间:2009-11-12 12:04:16

标签: mfc visual-c++

我想将tchar *转换为char *这是可能的。如果是的话怎么做我使用unicode设置

3 个答案:

答案 0 :(得分:2)

TCHAR可以是普通charwchar_t,具体取决于您的项目设置。如果是后者,则需要使用WideCharToMultiByte和相应的代码页参数。

答案 1 :(得分:2)

你无法转换指针,你需要分配一个新的字符串“char”而不是“wchar_t”

最优雅的方法是使用ATL转换宏,因为它会隐藏所有分配并调用其他注释中提到的函数

例如

#include <atlbase.h>
#include <atlconv.h>

void YourFunction() 
{ 
    TCHAR wszHelloTchar = _T("Hello!\n");

    USES_CONVERSION;   // a macro required once before using T2A() and other ATL macros

    // printf require a char*
    // T2A converts (if necessary) the TCHAR string to char
    printf( T2A( wszHelloTchar ) );
}

答案 2 :(得分:1)

我发现wcstombs非常适合做这类事情,