Jump to content
  • Advertisement
Sign in to follow this  
Hassanbasil

[C++] converting from LPSTR to TCHAR*

This topic is 3051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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");



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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!