Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting narrow string to wide string

How can i convert a narrow string to a wide string ?

I have tried this method :

string myName;
getline( cin , myName );
wstring printerName( L(myName) );  // error C3861: 'L': identifier not found
wchar_t* WprinterName = printerName.c_str(); // error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'wchar_t *'

But i get errors as listed above.

Why do i get these errors ? How can i fix them ?

Is there any other method of directly converting a narrow string to a wide string ?

like image 431
saplingPro Avatar asked Jul 14 '11 10:07

saplingPro


People also ask

How do you convert Lpcwstr to string?

This LPCWSTR is Microsoft defined. So to use them we have to include Windows. h header file into our program. To convert std::wstring to wide character array type string, we can use the function called c_str() to make it C like string and point to wide character string.

What is a Wstring?

std::wstring is used for wide-character/unicode (utf-16) strings. There is no built-in class for utf-32 strings (though you should be able to extend your own from basic_string if you need one).


1 Answers

If the source is ASCII encoded, you can just do this:

wstring printerName;
printerName.assign( myName.begin(), myName.end() );
like image 197
Blazes Avatar answered Oct 05 '22 23:10

Blazes