Sign in to follow this  

C++ ANSI and Unicode tips&tricks

Recommended Posts

leidegren    139
I'm wondering what you guys know about ANSI and Unicode strings in C++ ... For the most part if you write a quoted string "Hello World" it's a standard ANSI string right? Yet MS has some notation L"Hello World" to make it wide or unicode, and there's also some type TCHAR which seem to depdning on project settings... and don't forget the TEXT("Hello World") macro as well... How much of this is C++ standard, and how much is MS specific, or is it all MS specific? Also, i thought this could be a nice thread to disscuss localization issues and ANSI/Unicode string handling... I've currently set up my game so that all strings are defined by the compiler, and use single-byte or multi-byte strings depending on project settings... Should i scrapp this and just go with standard UTF-8 encoding? I've come to realize that UTF-8 uses a varible byte length depending on what type of character there is, and that it maps the ANSI character space exactly...

Share this post

Link to post
Share on other sites
jpetrie    13099
"this" is a narrow string.
L"this" is a wide string. This is standard C++, not Microsoft-specific.

Microsoft offers tchar.h, which contains the T() macro, which you can use to write code that can be narrow or wide depending on whether or not UNICODE is defined.

T("this") is "this" when UNICODE is not defined, and L"this" when it is.

The Win32 API generally uses T to indicate where a string can be either (at compile time) -- for example, LPTSTR, LPCSTR, LPWSTR. TCHAR is similarly char or wchar_t depending on whether UNICODE is defined or not.

Only L"" is standard C++, the rest is utility and helper stuff defined by Win32 API. Other APIs offer similar helper functionality, although I rarely find a need for it -- I generally simply only use Unicode nowadays, except when I have to deal with legacy APIs.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this