Convert std::string to WCHAR ?

Started by
20 comments, last by iMalc 10 years, 10 months ago

^ title

Advertisement
You might be able to use mbstowcs() to do so. If you gave more information, like what platform you are using then a more confident answer could be given.

I am trying to do this:


std::string s;

WCHAR w = s;

Also what do you mean what platform i am using?


std::string multi("Test");
std::wstring unicode(multi.begin(), multi.end());

This is the easiest form of multibyte to unicode conversion I know, won't even throw a compiler warning, unlike some solutions like std::copy. Then you can access the single characters of the wstring to get your WCHAR. Though given the little information and SiCranes answer which makes me a bit unsure, so please don't hit me if I misunderstood the actual problem ;)

EDIT:

Well given your next post it seems like it, but you can't simple do WCHAR = wstring eigther, as you can't do char = string. You rather want to do something amongst those lines, right?


std::wstring unicode; // suppose you used the method mentioned above to convert from string towstring
WCHAR w[256] = unicode.c_str() // you can only assign a (w)string to a (w)char array, not a single (w)char

I don't understand, the above code it giving me an error.

basically what i want is something like this


WCHAR convert(std::string s)
{
	WCHAR w;
	//do whatever needs to be done
	return w;
}


std::string s = "something";
WCHAR  w = convert(s);

A string is a sequence of zero or potentially many characters. A WCHAR is exactly one character. You cannot just cram a string into a single character.

What is the actual problem you're trying to solve?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

A string is a sequence of zero or potentially many characters. A WCHAR is exactly one character. You cannot just cram a string into a single character.

What is the actual problem you're trying to solve?

to pass a string in the second parameter here


WCHAR* filename = L"file path...";

D3DX11CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &texture, NULL);

This snippet always worked for me:

  std::wstring      ToStringW( const std::string& strText )
  {
    std::wstring      wstrResult;
    wstrResult.resize( strText.length() );
    typedef std::codecvt<wchar_t, char, mbstate_t> widecvt;
    std::locale     locGlob;
    std::locale::global( locGlob );
    const widecvt& cvt( std::use_facet<widecvt>( locGlob ) );
    mbstate_t   State;
    const char*       cTemp;
    wchar_t*    wTemp;
    cvt.in( State,
            &strText[0], &strText[0] + strText.length(), cTemp,
            (wchar_t*)&wstrResult[0], &wstrResult[0] + wstrResult.length(), wTemp );
               
    return wstrResult;
  }

Then use .c_str() on the std::wstring to get a WCHAR* like.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Here is one nice small snippet.


const wchar_t* to_wide( const std::string& strToConvert ) {
  return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str();
}
 

Notice: not working, skip it. I just leave it as is, to be here as warning how NOT to do some things.

Here is one nice small snippet.


const wchar_t* to_wide( const std::string& strToConvert ) {
  return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str();
}
 


This code is deeply broken. You're returning a pointer to memory that is freed when the function exits (because the temporary wstring will be destructed). It may seem to work, but that's the horrifying reality of undefined behavior - it often seems to work until it bites you viciously.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement