By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

Can I get a wchar_t from a std::string?

P: n/a
I can see how to get a char* but is it possible to get a wide char -
eg
wchar_t?

Feb 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 16, 1:00 pm, "Angus" <anguscom...@gmail.comwrote:
I can see how to get a char* but is it possible to get a wide char -
eg
wchar_t?
Get from where?

--
Erik Wikström

Feb 16 '07 #2

P: n/a
On Feb 16, 5:00 pm, "Angus" <anguscom...@gmail.comwrote:
I can see how to get a char* but is it possible to get a wide char -
eg
wchar_t?
std::string is a specialization of class basic_string with elements of
type char as a string.Hence std::string::c_str() would return you
char*. You can use MultiByteToWideChar function to convert this char
string into wchar_t string.

Instead use std::wstring which is a specialization of class
basic_string with elements of type wchar_t as a string and
std::wstring::c_str() would return you wchar_t*.You can use
WideCharToMultiByte function to convert this wide char string into
char string.

Feb 16 '07 #3

P: n/a
On 16 Feb, 14:14, "Uday Bidkar" <uday.bid...@gmail.comwrote:
On Feb 16, 5:00 pm, "Angus" <anguscom...@gmail.comwrote:
I can see how to get a char* but is it possible to get a wide char -
eg
wchar_t?

std::string is a specialization of class basic_string with elements of
type char as a string.Hence std::string::c_str() would return you
char*. You can use MultiByteToWideChar function to convert this char
string into wchar_t string.

Instead use std::wstring which is a specialization of class
basic_string with elements of type wchar_t as a string and
std::wstring::c_str() would return you wchar_t*.You can use
WideCharToMultiByte function to convert this wide char string into
char string.
MultiByteToWideChar and WideCharToMultiByte are not standard C++
functions. If they are provided by your implementation then they are
an extension. If they do what you need then use them, but understand
that the next compiler you use may not provide them.

Gavin Deane

Feb 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.