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

Discussion in 'C++' started by Angus, Feb 16, 2007.

  1. Angus

    Angus Guest

    I can see how to get a char* but is it possible to get a wide char -
    eg
    wchar_t?
    Angus, Feb 16, 2007
    #1
    1. Advertising

  2. On Feb 16, 1:00 pm, "Angus" <> wrote:
    > 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 16, 2007
    #2
    1. Advertising

  3. Angus

    Uday Bidkar Guest

    On Feb 16, 5:00 pm, "Angus" <> wrote:
    > 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.
    Uday Bidkar, Feb 16, 2007
    #3
  4. Angus

    Gavin Deane Guest

    On 16 Feb, 14:14, "Uday Bidkar" <> wrote:
    > On Feb 16, 5:00 pm, "Angus" <> wrote:
    >
    > > 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
    Gavin Deane, Feb 16, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sorty
    Replies:
    4
    Views:
    20,706
    Rolf Magnus
    Nov 25, 2003
  2. Peter Jansson
    Replies:
    5
    Views:
    6,272
    Ivan Vecerina
    Mar 17, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    434
  4. Jeffrey Walton
    Replies:
    10
    Views:
    929
    Mathias Gaunard
    Nov 26, 2006
  5. Replies:
    3
    Views:
    1,086
    James Kanze
    Aug 15, 2008
Loading...

Share This Page