Results 1 to 4 of 4

Thread: [C++] Convert char* to wchar_t

  1. #1
    Join Date
    May 2008
    Beans
    1,029

    Question [C++] Convert char* to wchar_t

    I am trying to convert a char string to a wide char string.

    PHP Code:
    #include <iostream>
    using namespace std;

    int main(int argccharargv[])
    {
        if (
    argc 1)
            {
            
    wchar_targ1 = (wchar_t*) argv[1];
            
    cout << arg1 << endl;
            return 
    0;
        }
        else return 
    1;

    Apart from the above, I have also tried these:
    PHP Code:
    wchar_targ1 argv[1]; 
    http://www.cplusplus.com/reference/c...dlib/mbstowcs/
    PHP Code:
    wchar arg1[512];
    mbtowcs(arg1argv[1], 512); 
    I have also seen some suggest using "wcout" instead of cout:
    PHP Code:
    wcout << arg1 << endl
    but "wcout" isn't found:
    Code:
    : error: `wcout' was not declared in this scope
    Last edited by dodle; September 22nd, 2010 at 10:57 AM.

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: [C++] Convert char* to wchar_t

    After a few moments of Googling, I found this (partial) solution:
    Code:
    #include <locale>
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std ;
    
    wstring widen( const string& str )
    {
        wostringstream wstm ;
        const ctype<wchar_t>& ctfacet = 
                            use_facet< ctype<wchar_t> >( wstm.getloc() ) ;
        for( size_t i=0 ; i<str.size() ; ++i ) 
                  wstm << ctfacet.widen( str[i] ) ;
        return wstm.str() ;
    }
    
    string narrow( const wstring& str )
    {
        ostringstream stm ;
        const ctype<char>& ctfacet = 
                             use_facet< ctype<char> >( stm.getloc() ) ;
        for( size_t i=0 ; i<str.size() ; ++i ) 
                      stm << ctfacet.narrow( str[i], 0 ) ;
        return stm.str() ;
    }
    
    int main()
    {
      {
        const char* cstr = "abcdefghijkl" ;
        const wchar_t* wcstr = widen(cstr).c_str() ;
        wcout << wcstr << L'\n' ;
      }
      {  
        const wchar_t* wcstr = L"mnopqrstuvwx" ;
        const char* cstr = narrow(wcstr).c_str() ;
        cout << cstr << '\n' ;
      } 
    }
    The only caveat I have found with the code above is that second code-block in the main() function fails to perform its job. I think it has something to do with the locale used by the ostringstream.

    But if all you want is to go from char* to wchar_t*, then just utilize the widen() function, as shown above.

  3. #3
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: [C++] Convert char* to wchar_t

    I presume the reason you want to go to wide chars is that you want to use non ascii. For this it is is handy to have a function that can read utf-8 sequences to make a wide char. Do beware though some wide char implementations are apparently not wide enough for unicode so I ended up making my own unicode type.

    Someone told me there are standard library routines for converting to and from utf-8 but I had already written my own by then, but just in case you want to have a look anyway I'll attach my source code.
    Attached Files Attached Files

  4. #4
    Join Date
    May 2008
    Beans
    1,029

    Re: [C++] Convert char* to wchar_t

    Sorry, I forgot to post that I had found a solution. I used the C++ function mbstowcs:

    PHP Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    int main(int argccharargv[])
    {
        if (
    argc 1)
        {
            
    wchar_t arg1[512];
            
    mbstowcs(arg1argv[1], 512);
            
    wcout << arg1 << endl;
            return 
    0;
        }
        else return 
    1;

    The reason why I needed to convert to wide char was because I was doing some coding with WINAPI fuctions that required unicode strings.
    Last edited by dodle; September 23rd, 2010 at 01:01 PM. Reason: added "#include <stdlib.h>"

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •