Jump to content
  • Advertisement
Sign in to follow this  
ProgrammerZ

convert char* to LPCWSTR

This topic is 3695 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

Hey, I was just wondering if there is an easy way to convert a char* to a LPCWSTR. I'd also like to know how to convert a char* to a std::string, a LPCWSTR to a std::string, a std::string to a char*, etc. --ProgrammerZ

Share this post


Link to post
Share on other sites
Advertisement

// Convert char_array to std::string (It is done automatically with the strings = overload)
char* char_array = "Foo";
std::string str = char_array;

// Convert std::string to char array
std::string str = "Bar";
char* char_array = str.c_str();


I really have no idea about LPCWSTR...

Edit:
It looks like the LPCWSTR is just a wide character char array. I'm not really sure why you would need to convert back and forth. Generally you would use either Single byte characters or Multibyte. You can choose to always use Multibyte in the Project properties in Visual Studio IIRC.

Share this post


Link to post
Share on other sites
A LPCWSTR is a "Long Pointer to Const Wide String". To convert from a multibyte string (char *) to a unicode string (wchar_t *) you will need to use the function mbstowcs or MultiByteToWideChar.

This page should have the examples you need for a MultiByteToWideChar example. This page on MSDN has an example for mbstowcs.

To get a LPCWSTR to a std::string, you convert it to a multibyte string first (char *) and then save the std::string from the char * as Sr_Guapo has shown, but you have to be careful of the 'const' pointer conversions and type cast yourself.

Share this post


Link to post
Share on other sites
Adapt these as you see fit.


// ----------------------------------------------------------------------------
//
//
BOOL WINAPI UnicodeToAnsi(
LPWSTR pszwUniString,
LPSTR pszAnsiBuff,
DWORD dwAnsiBuffSize
)
{
int iRet = 0;
iRet = WideCharToMultiByte(
CP_ACP,
0,
pszwUniString,
-1,
pszAnsiBuff,
dwAnsiBuffSize,
NULL,
NULL
);
return ( 0 != iRet );
}


// ----------------------------------------------------------------------------
//
//
BOOL WINAPI AnsiToUnicode(
LPSTR pszAnsiString,
LPWSTR pszwUniBuff,
DWORD dwUniBuffSize
)
{

int iRet = 0;
iRet = MultiByteToWideChar(
CP_ACP,
0,
pszAnsiString,
-1,
pszwUniBuff,
dwUniBuffSize
);

return ( 0 != iRet );
}

Share this post


Link to post
Share on other sites
char* to LPCWSTR is going to be quite painful. I suggest using a wstring wherever you can, and using wstring::c_str() to convert to LPCWSTR (which is just a const wchar_t*) whenever you need it.


std::wstring ToWideString(const std::string& str)
{
int stringLength = MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), 0, 0);
std::wstring wstr(stringLength, 0);
MultiByteToWideChar(CP_ACP, 0, str.data(), str.length(), &wstr[0], stringLength);
return wstr;
}


That's untested and written off the top of my head, but it should work (I think). If not, that's the general gist of it.

EDIT: Wow, I'm slow. [lol]

Share this post


Link to post
Share on other sites
Because the ASCII character set is a subset of most wide character sets, and still have the values 0-255 (the higher-order byte is just 0), this would work.

std::string charstring = "hello, world";

std::wstring widestring;

for (int i = 0; i < (int)charstring.length(); i++)
widestring += (wchar_t)charstring;

LPCWSTR lpcwstr = widestring.c_str();


Although, LessBread's method is probably safer to use.

Share this post


Link to post
Share on other sites
You want to use "wchar_t" instead of "char" if you need a LPCWSTR. LPCWSTR is a pointer to a wide char array (Long Pointer to Const Wide String). You can also change your project to not use Unicode or use _TCHAR instead of "char" which will automatically become "wchar_t" if unicode is defined and "char" if unicode is not defined.

Share this post


Link to post
Share on other sites
You can also use L as a prefix to identify a wchar_t* string literal.

Eg:
const wchar_t* some_text = L"blah";
if(some_text[3] != wchar_t('a')) throw wobbly;

NOTE:
I'm not sure where this feature is defined.
It works in MSVC, I believe it works in g++.
It's probably part of the C++ standard somewhere but I'm not 100% sure where to look.

ASIDE:
I've seen that there are proposals for C++0x for the syntax:
int* digits = <int>"510034222";
or
int* digits = int"510034222";
Would be great to have this feature in C++.


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!