Hello there, do you have any idea of what is the corresponding standard lib function of Win32's WideCharToMultiByte()? Thanks!

9 Years
Discussion Span
Last Post by jaepi

>> what is the corresponding standard lib function of Win32's WideCharToMultiByte()?
there are two options available; use the ctype<> facet or the codecvt<> facet. the following example uses ctype<> for conversion.

#include <locale>
#include <iostream>
#include <string>
#include <sstream>
using namespace std ;

wstring widen( const string& str, const locale& loc = locale::classic() )
    wostringstream wstm ;
    wstm.imbue(loc) ;
    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, const locale& loc = locale::classic() )
    ostringstream stm ;
    stm.imbue(loc) ;
    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 string str = "abcdefghijkl" ;
    const wstring wstr = widen(str) ; // use clasic C locale
    wcout << wstr << L'\n' ;
    const wstring wstr = L"mnopqrstuvwx" ;
    const string cstr = narrow( wstr, locale("POSIX") ) ;
    cout << cstr << '\n' ;

>> In addition the CreateEvent() function.
there is no threading support in c++98. a proposal for a thread library (based on boost.thread) is part of TR2, yet to be accepted into c++0x. for a portable solution, you could use boost.thread. but the equivalent of win32 Event mechanism is not provided by the library; instead java style condition variables are available. this would require some reworking of your code, but would probably result in a solution that has more technical merit. see http://www.boost.org/doc/html/thread/rationale.html#thread.rationale.events

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.