> How do I convert a LPWSTR (wchar_t*) to an std:string?
perhaps you want a wstring instead? it's a string specialized for
wchar_t
from help for basic_string:
A templatized class for handling sequences of character-like entities.
string and wstring are specialized versions of basic_string for char’s
and wchar_t’s, respectively.
typedef basic_string <char> string;
typedef basic_string <wchar_t> wstring;
--
Good luck,
liz
STL offers two standard versions of string. One that supports char and
another that supports wchar_t.
If you just want to transfer raw unicode string into STL class than use
std::wstring. If, on the
other hand you need to use std::string than conversion can be performed
like this(on windows only of course):
void convert(LPWSTR input, std::string& out) {
if ( NULL != input ) {
int szin = wcslen(input);
if (szin > 0) {
int size = ::WideCharToMultiByte( CP_ACP,
0 ,
input ,
szin ,
NULL ,
0 ,
NULL ,
NULL );
if ( (size > 0) ) {
std::auto_ptr<char> resultP(new char[size+1]);
if ( 0 != ::WideCharToMultiByte( CP_ACP,
0,
input,
szin,
resultP.get(),
size,
NULL,
NULL ) )
{
resultP.get()[size] = 0;
out = resultP.get();
}
}
}
}
}
Darko