Unicode, TCHAR, STL std::string & std::stringw and building in mode x64

When you build a Visual Studio project, you have to consider multiple project options:

The Character Set option is very important in the way you will write code. Microsoft wants every developer to build with “Use Unicode Character Set” but bad habits were taken by developers.
They have preferred the option “Use Multi-Byte Character Set (MBCS)” where strings are things you put in “” and that’s all !

So what is the problem Doctor ? Handling Unicode allows you to target multiples languages and it’s the way we build modern softwares.
To help you achieve your migration or your new software, Microsoft provides TCHAR.h with a lot of macros:

– TCHAR means char in MBCS and wchar_t in Unicode

– _T(“toto”) means “toto” in MBCS and L”toto” in Unicode
To properly call routines of the CRT using TCHAR, you have to notice the name of the functions that care of TCHAR : https://docs.microsoft.com/en-us/cpp/c-runtime-library/routine-mappings Example: printf -> Unicode: wprintf, TCHAR: _tprintf
If you use _tprintf and #include , the function _tprintf will be expanded using the right function name.

TCHAR szLisa[10] = _T(“Lisa”);
_tprintf(_T(“%s\n”), szLisa);

But the question is : How do I switch from one mode to another. Example, I use STL, I can have std::string or std::wstring. Using STL strings has a mode that let you recover the characters using the c_str() function.

std::wstring s1 = szLisa;
std::wcout << s1 << std::endl;

If you want to dump from one world to another, the STL has a way:

std::string s2(s1.begin(), s1.end());
std::cout << s2 << std::endl;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: