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. Advertisements

  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. Advertisements

  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. Advertisements

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. Matthias =?ISO-8859-1?Q?K=E4ppler?=

    std::string::push_back vs. std::string::operator+=

    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 22, 2004, in forum: C++
    Replies:
    2
    Views:
    4,474
    Jonathan Mcdougall
    Nov 23, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,985
    Ivan Vecerina
    Mar 17, 2005
  3. Jim Langston

    std::string = char* + std::string

    Jim Langston, Jun 30, 2005, in forum: C++
    Replies:
    9
    Views:
    594
    Jim Langston
    Jul 2, 2005
  4. Fei Liu
    Replies:
    9
    Views:
    696
  5. Jeffrey Walton
    Replies:
    10
    Views:
    1,262
    Mathias Gaunard
    Nov 26, 2006
  6. Christopher Pisz
    Replies:
    11
    Views:
    669
    James Kanze
    Jan 13, 2008
  7. Replies:
    3
    Views:
    1,429
    James Kanze
    Aug 15, 2008
  8. Dwight Army of Champions
    Replies:
    0
    Views:
    537
    Dwight Army of Champions
    Apr 20, 2010
Loading...