Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LPWSTR to std::string

10 views
Skip to first unread message

Stephane Baillargeon

unread,
Sep 25, 2004, 4:29:02 PM9/25/04
to
How do I convert a LPWSTR (wchar_t*) to an std:string?


Liz Albin

unread,
Sep 25, 2004, 7:48:06 PM9/25/04
to
On Sat, 25 Sep 2004 16:29:02 -0400, Stephane Baillargeon wrote:

> 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

Darko Miletic

unread,
Sep 26, 2004, 2:15:19 PM9/26/04
to
Stephane Baillargeon wrote:
> How do I convert a LPWSTR (wchar_t*) to an std:string?
>
>

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

0 new messages