I want to convert std::string into a const wchar_t *
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*
.
char* to const wchar_t * conversion
What's the type of getIpAddress()
?
Here's a snippet showing how to convert a string
to a wstring
(using standard C++11, no error checking):
wstring to_wstring(string const& str)
{
size_t len = mbstowcs(nullptr, &str[0], 0);
if (len == -1) // invalid source str, throw
wstring wstr(len, 0);
mbstowcs(&wstr[0], &str[0], wstr.size());
return wstr;
}
Unexpected results with wchar_t and c_str string conversion
Each time you call GetWideString()
, you are creating a new std::wstring
, which has a newly allocated memory buffer. You are comparing pointers to different memory blocks (assuming Assert::AreEqual()
is simply comparing the pointers themselves and not the contents of the memory blocks that are being pointed at).
Update: const wchar_t* wchar2 = GetWideString("me").c_str();
does not work because GetWideString()
returns a temporary std::wstring
that goes out of scope and gets freed as soon as the statement is finished. Thus you are obtaining a pointer to a temporary memory block, and then leaving that pointer dangling when that memory gets freed before you can use the pointer for anything.
Also, const wchar_t* wchar2 = GetWChar("me");
should not compile. GetWChar()
returns a std::wstring
, which does not implement an implicit conversion to wchar_t*
. You have to use the c_str()
method to get a wchar_t*
from a std::wstring
.
How to make String to const wchar_t* conversion function work under Windows and Linux
In my opinion, there are 2 ways :
- convert QString to wchar_t* when needed
- Let QString to store wchar_t* and return QString::unicode directly
These two functions can convert a QString to std::string and std::wstring
QString::toStdWString
QString::toStdString
To build QString as ucs4 :
#define QT_QSTRING_UCS_4
#include "qstring.h"
This can be used in qt3(qstring.h). I can't find the source of qt4.
How to convert a std::string to L data type
auto
is not a data-type. It is a placeholder that gets deduced depending on the initializer used.
In your case, the initializer is a wide string-literal of type const wchar_t[size]
, which decays to const wchar_t*
when used to initialize the variable.
A wide string can be stored in a std::wstring
.
How to convert a std::string
(a narrow string) to a wide string depends on the source's character encoding.
Anyway, there are many others who asked that too:
C++ Convert string (or char*) to wstring (or wchar_t*)
wcscpy_s not affecting wchar_t*
Try something more like this:
struct datum {
wchar_t* name;
wchar_t* lore;
};
wchar_t* widen(const char *str)
{
wchar_t *wBuffer = NULL;
size_t len = strlen(str) + 1;
size_t wlen = 0;
mbstowcs_s(&wlen, NULL, 0, str, len);
if (wlen)
{
wBuffer = new wchar_t[wlen];
mbstowcs_s(NULL, wBuffer, wlen, str, len);
}
return wBuffer;
}
datum thisDatum;
thisDatum.name = widen((const char*)sqlite3_column_text(pStmt, 1));
thisDatum.lore = widen((const char*)sqlite3_column_text(pStmt, 2));
...
delete[] thisDatum.name;
delete[] thisDatum.lore;
That being said, I would use std::wstring
instead:
struct datum {
std::wstring name;
std::wstring lore;
};
#include <locale>
#include <codecvt>
std::wstring widen(const char *str)
{
std::wstring_convert< std::codecvt<wchar_t, char, std::mbstate_t> > conv;
return conv.from_bytes(str);
}
datum thisDatum;
thisDatum.name = widen((const char*)sqlite3_column_text(pStmt, 1));
thisDatum.lore = widen((const char*)sqlite3_column_text(pStmt, 2));
Related Topics
Alternative Function in iOStream.H for Getch() of Conio.H
Detect When Multiple Enum Items Map to Same Value
How to Create a Window in Different Qt Threads
C++ Constant Reference Lifetime (Container Adaptor)
How to Make Gcc Compile the .Text Section as Writable in an Elf Binary
Is There Any Compiler Barrier Which Is Equal to Asm("" ::: "Memory") in C++11
What Does "Operator = Must Be a Non-Static Member" Mean
I Want a Vector of Derived Class Pointers as Base Class Pointers
Why Does Calling Method Through Null Pointer "Work" in C++
Error: Cannot Bind Non-Const Lvalue Reference of Type 'Int&' to an Rvalue of Type 'Int'
What Is/Are the Purpose(S) of Inline
How to Pass a Member Function to a Function Pointer
Standard Conversions: Array-To-Pointer Conversion
Generating an Interface Without Virtual Functions
Assign a Nullptr to a Std::String Is Safe
What Is the Best Practice for Combating the Console Closing Issue