Convert const char* to const wchar_t*

Giaphage47

I am trying to create a program with Irrlicht that loads certain things from a configuration file written in Lua, one of which is the window title. However, the lua_tostring function returns a const char* while the Irrlicht device's method setWindowCaption expects a const wchar_t*. How can I convert the string returned by lua_tostring?

R Sahu

There are multiple questions on SO that address the problem on Windows. Sample posts:

  1. char* to const wchar_t * conversion
  2. conversion from unsigned char* to const wchar_t*

There is a platform agnostic method posted at http://ubuntuforums.org/showthread.php?t=1579640. The source from this site is (I hope I am not violating any copyright):

#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 ;

    // Incorrect code from the link
    // const ctype<char>& ctfacet = use_facet<ctype<char>>(stm.getloc());

    // Correct code.
    const ctype<wchar_t>& ctfacet = use_facet<ctype<wchar_t>>(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' ;
  } 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot convert 'const char*' to 'WCHAR*' in argument passing

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

Convert const char** to void*?

cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

VS 2017 Doesn't Implicitly Convert const char* to char*

How to convert TCHAR to const char?

How to convert int variable and append to const wchar_t*

cannot convert from const char* to const char *&

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

How to convert char to wchar_t*?

(const char*)++ or (const char)++?

Convert const char* to QString

Cannot convert 'TCHAR*' to 'const char*'

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

C++ wifstream: Incompatible type char const*, wchar_t const*

Convert LOWORD(wParam) to const wchar_t*

Convert const wchar_t* into a WCHAR*

Make a condition on const wchar_t*

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

Can't convert QString to Const Char*

String parameter const char* and const wchar_t*

Convert a QString to a const char*

C++ Builder 10.3 can not assign to const wchar_t* from const char[18]

Convert const wchar_t* to LPWSTR

Is " const wchar_t* " a pointer?

How to use regex_iterator with const char/wchar_t*?

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()