Char in MFC

character

  • char:
    prefix with str
    ANSI: strcat( ), strcpy( ), strlen( )
  • wchar_t :
    prefix with wcs
    Unicode: wcscat(), wcscpy(), wcslen()
  • WCHAR:
    typedef wchar_t WCHAR
    WCHAR == wchar_t
    macro _UNICODE in c, macro UNICODE in windows
    if no macro defined, system use ANSI compiling and run.
  • TCHAR:
    Condition define type
    If macro UNICODE defined
    typedef wchar_t TCHAR;
    If not
    typedef char TCHAR;

checkout

1
2
3
4
5
6
7
8
#ifdef   UNICODE
typedef wchar_t TCHAR;
#else
typedef unsigned char TCHAR;
#endif

typedef unsigned char CHAR;
typedef unsigned wchar_t WCHAR;

From VS2005 C++’s encoding use UNICODE in default.

translate

char -> wchar_t

1
2
3
4
5
6
7
8
9
wchar_t* Util::CharToWchar(char* c)
{
int length = strlen(c) + 1; // the text end \0 [0x00] [EOF]
wchar_t *wide = (wchar_t*)malloc(sizeof(wchar_t)*length);
memset(t, 0, length * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, c, strlen(c), wide, length);
//wcout << wide << endl;
return wide;
}

wchar_t -> char

1
2
3
4
5
6
7
8
9
10
char* Util::WcharToChar(wchar_t* wc)
{
int length = wcslen(wc) + 1;
cout << length1 << endl;
char *c = new char[length1];
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, wc, -1,
c, length, NULL, NULL);
cout << c << endl;
return c;
}