简体   繁体   English

将 const char* 转换为 const wchar_t*

[英]Convert const char* to const wchar_t*

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.我正在尝试使用 Irrlicht 创建一个程序,该程序从用 Lua 编写的配置文件中加载某些内容,其中之一是窗口标题。 However, the lua_tostring function returns a const char* while the Irrlicht device's method setWindowCaption expects a const wchar_t* .然而, lua_tostring函数返回一个const char*而 Irrlicht 设备的方法setWindowCaption需要一个const wchar_t* How can I convert the string returned by lua_tostring ?如何转换lua_tostring返回的字符串?

There are multiple questions on SO that address the problem on Windows. SO 上有多个问题可以解决 Windows 上的问题。 Sample posts:示例帖子:

  1. char* to const wchar_t * conversion char* 到 const wchar_t * 转换
  2. conversion from unsigned char* to const wchar_t* 从 unsigned char* 到 const wchar_t* 的转换

There is a platform agnostic method posted at http://ubuntuforums.org/showthread.php?t=1579640 .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' ;
  } 
}

You can use mbstowcs :您可以使用mbstowcs

    wchar_t WBuf[100];
    mbstowcs(WBuf,lua_tostring( /*...*/ ),99);

or more safe:或更安全:

    const char* sz = lua_tostring(/*...*/);
    std::vector<wchar_t> vec;
    size_t len = strlen(sz);
    vec.resize(len+1);
    mbstowcs(&vec[0],sz,len);
    const wchar_t* wsz = &vec[0];

For Unicode:对于 Unicode:

std::string myString = "Master James";
const char* sz = myString.c_str();
size_t origsizes = strlen(sz) + 1;
const size_t newsizes = 500;
size_t convertedCharP = 0;
wchar_t constTowchar[500];
mbstowcs_s(&convertedCharP, constTowchar, origsizes, sz, _TRUNCATE);
std::wcout << constTowchar << std::endl;

This is working using mbstowcs_s .这是使用mbstowcs_s工作的。

#include <xlocale>
#include <sstream>

// do not forget about std::locale::global(std::locale("")); for some characters

[[nodiscard]] inline std::wstring widen(const char* str)
{
    const auto length = strlen(str);

    std::wstring result;
    result.resize(length);

    const auto& facet = use_facet<std::ctype<wchar_t>>(std::wostringstream().getloc());
    std::transform(str, str + length, result.begin(), [&facet](const char ch)
    {
        return facet.widen(ch);
    });
    return result;
}

[[nodiscard]] inline std::string narrow(const wchar_t* str)
{
    const auto length = wcslen(str);

    std::string result;
    result.resize(length);

    const auto& facet = use_facet<std::ctype<wchar_t>>(std::ostringstream().getloc());
    std::transform(str, str + length, result.begin(), [&facet](const wchar_t ch)
    {
        return facet.narrow(ch);
    });
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM