简体   繁体   中英

How to convert wchar_t[] to basic_string<_Elem>?

I need to convert a project from VS2003 to VS2008. In the following code:

wchar_t wpom[30];
mbtowc(wpom, "olaboga", 10);

ati_dom::DOMString w = wpom;

I get an error (on the last line): Cannot convert from 'wchar_t[30]' to 'basic_string<_Elem>').

I tried to modify it to:

wchar_t wpom[30];
mbtowc(wpom, "olaboga", 10);

std::basic_string<wchar_t> basic_wpom(wpom);
ati_dom::DOMString w = basic_wpom;

But all I acomplished is to get another error: Cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem>'

How can I convert wchar_t[] to basic_string<_Elem> and not to basic_string<_Elem,_Traits,_Ax>...?

Just use a std::wstring directly via the constructor that takes a pointer to the first element and the length of the array:

wchar_t warr[ 30 ];
// populate the array
std::wstring wstrTemp( &warr[ 0 ], 30 );

Sorry for not responding - i found out what the problem was on my own.

As it turns out, DOMString is declared like this:

typedef std :: basic_string< unsigned short > DOMString;

So simple casting to unsigned short did the trick:

ati_dom::DOMString w = (unsigned short *)wpom;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM