How to convert tchar pointer to char pointer

2019-07-09 04:18发布

I want to conver a tchar* to char * is this possible . if yes how to do it. I use unicode setting

3条回答
贪生不怕死
2楼-- · 2019-07-09 04:37

I find wcstombs works great for doing this sort of thing,

查看更多
【Aperson】
3楼-- · 2019-07-09 04:38

You can't convert the pointer, you need to allocate a new string that is "char" instead of "wchar_t"

the most elegant way to do this is with the ATL conversion macros because it will hide all the allocation and called to the functions mentioned in the other comments

example

#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 ) );
}
查看更多
疯言疯语
4楼-- · 2019-07-09 04:41

A TCHAR is either a plain char or a wchar_t depending on your project's settings. If it's the latter, you would need to use WideCharToMultiByte with appropriate code page parameter.

查看更多
登录 后发表回答