convert char* to LPCWSTR
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
// 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 arraystd::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.
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.
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.
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 );}
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.
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]
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]
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.
Although, LessBread's method is probably safer to use.
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.
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.
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++.
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++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement