user25749
user25749

Reputation: 4885

I want to convert std::string into a const wchar_t *

Is there any method? My computer is AMD64.

::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);

When I used:

loadU(&str);

the VS2005 compiler says:

Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'

How can I do it?

Upvotes: 71

Views: 193771

Answers (6)

starriet 차주녕
starriet 차주녕

Reputation: 3908

If one can use CString, leveraging CString::GetString() would be convenient.

    #include <atlstr.h> // to use CString.
    std::string s = "std::string 한글";
    std::wstring ws = static_cast<CString>(s.c_str()).GetString();

E.g. using MFC in Windows,

    std::string s = "std::string 한글";

    std::wstring ws1 = std::wstring(s.cbegin(), s.cend()); // Uh-oh.
    ::AfxMessageBox(ws1.c_str()); // does NOT print correctly.

    std::wstring ws2 = static_cast<CString>(s.c_str()).GetString();
    ::AfxMessageBox(ws2.c_str()); // prints correctly.
    
    // some more examples.
    ::AfxMessageBox(static_cast<CString>(s.c_str()).GetString()); // prints correctly. 
    ::AfxMessageBox(static_cast<CString>(s.c_str())); // Also works without GetString().
    
    // this is fine.
    std::wstring ws3 = L"std::wstring 한글";
    ::AfxMessageBox(ws3.c_str()); // prints correctly.

Upvotes: 0

Grantg
Grantg

Reputation: 11

Need to pass a wchar_t string to a function and first be able to create the string from a literal string concantenated with an integer variable.

The original string looks like this, where 4 is the physical drive number, but I want that to be changeable to match whatever drive number I want to pass to the function

auto TargetDrive = L"\\\\.\\PhysicalDrive4";

The following works

int a = 4;


std::string stddrivestring = "\\\\.\\PhysicalDrive" + to_string(a);

std::wstring widedrivestring = std::wstring(stddrivestring.begin(), stddrivestring.end());

const wchar_t* TargetDrive = widedrivestring.c_str();

Upvotes: 0

marijne
marijne

Reputation: 3060

First convert it to std::wstring:

std::wstring widestr = std::wstring(str.begin(), str.end());

Then get the C string:

const wchar_t* widecstr = widestr.c_str();

This only works for ASCII strings, but it will not work if the underlying string is UTF-8 encoded. Using a conversion routine like MultiByteToWideChar() ensures that this scenario is handled properly.

Upvotes: 116

Matt Dillard
Matt Dillard

Reputation: 14853

If you have a std::wstring object, you can call c_str() on it to get a wchar_t*:

std::wstring name( L"Steve Nash" );
const wchar_t* szName = name.c_str();

Since you are operating on a narrow string, however, you would first need to widen it. There are various options here; one is to use Windows' built-in MultiByteToWideChar routine. That will give you an LPWSTR, which is equivalent to wchar_t*.

Upvotes: 50

kriss
kriss

Reputation: 24177

If you are on Linux/Unix have a look at mbstowcs() and wcstombs() defined in GNU C (from ISO C 90).

  • mbs stand for "Multi Bytes String" and is basically the usual zero terminated C string.

  • wcs stand for Wide Char String and is an array of wchar_t.

For more background details on wide chars have a look at glibc documentation here.

Upvotes: 5

Rob
Rob

Reputation: 78638

You can use the ATL text conversion macros to convert a narrow (char) string to a wide (wchar_t) one. For example, to convert a std::string:

#include <atlconv.h>
...
std::string str = "Hello, world!";
CA2W pszWide(str.c_str());
loadU(pszWide);

You can also specify a code page, so if your std::string contains UTF-8 chars you can use:

CA2W pszWide(str.c_str(), CP_UTF8);

Very useful but Windows only.

Upvotes: 10

Related Questions