Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


convert char* to LPCWSTR


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 ProgrammerZ   Members   -  Reputation: 180

Like
0Likes
Like

Posted 09 July 2008 - 11:15 AM

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

Sponsor:

#2 Sr_Guapo   Members   -  Reputation: 876

Like
0Likes
Like

Posted 09 July 2008 - 11:31 AM


// 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.

#3 Drew_Benton   Crossbones+   -  Reputation: 1733

Like
0Likes
Like

Posted 09 July 2008 - 11:39 AM

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.

#4 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 09 July 2008 - 11:41 AM

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



#5 Sc4Freak   Members   -  Reputation: 643

Like
0Likes
Like

Posted 09 July 2008 - 11:49 AM

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]

#6 tendifo   Members   -  Reputation: 146

Like
0Likes
Like

Posted 09 July 2008 - 11:54 AM

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[i];

LPCWSTR lpcwstr = widestring.c_str();

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

#7 1Bad   Members   -  Reputation: 122

Like
0Likes
Like

Posted 09 July 2008 - 11:56 AM

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.

#8 yacwroy   Members   -  Reputation: 123

Like
0Likes
Like

Posted 09 July 2008 - 05:17 PM

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++.




#9 MJP   Moderators   -  Reputation: 14022

Like
0Likes
Like

Posted 09 July 2008 - 05:44 PM

Time for another shameless plug for my journal entry on Unicode.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS