[C++] converting from LPSTR to TCHAR*

Started by
3 comments, last by Hassanbasil 14 years, 1 month ago
Hello everyone, I'm trying to convert my LPSTR to TCHAR*, but i can't seem to find a way to do it, is it possible? i would also like to know, why is TCHAR's and WCHAR's used for, anyway?
Advertisement
There are basically two kinds of character types supported in Windows (the type to use is usually decided by the compiler): CHAR and WCHAR.

A CHAR is typically stored as an 8-bit character (either ASCII or a multibyte set). A WCHAR is typically stored as a 16-bit character (a wide character, effectively UTF-16).

A TCHAR is basically a macro in place that converts to whichever character mode you've got your compiler in. If it's in ASCII/Multibyte, a TCHAR translates to a CHAR type. If it's in Unicode, it will translate to a WCHAR. You'll find that just about any win32 function that takes text input of some kind actually has two versions of the function, one that ends in A and one that ends in W. The A is the ASCII/Multibyte version. The W is the wide character version. These functions are typically hidden away by a macro that translates to the appropriate function based on the compiler settings.

If your compiler is set to Unicode, then it isn't as simple as casting from LPSTR to TCHAR*. You are going to need to use a conversion function, like MultiByteToWideChar Function

In general, the safest way to handle text in windows to to declare your strings as TCHARs and use the TEXT() macro when declaring the text. It will handle translate the data to the appropriate form (at compiler time). Note this is only for data available at compile-time, not user input. User input may require translation, depending on the source.

TCHAR _SomeText = TEXT("MyText");

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

i need it for user input, it's sad that the function i need only have W version, not A, anyway, i think i found another way to do it without going into tchars and stuff, thank you for explaining through !
Quote:Original post by Hassanbasil
anyway, i think i found another way to do it without going into tchars and stuff, thank you for explaining through !


Can you explain which solution you've decided to use?

i didnt use any solution, as it looked a bit confusing, i just found some other functions to do the job, without TCHARs

This topic is closed to new replies.

Advertisement