Sign in to follow this  
Hassanbasil

[C++] converting from LPSTR to TCHAR*

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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");



Share this post


Link to post
Share on other sites
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 !

Share this post


Link to post
Share on other sites
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?

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