I have a wchar array I need to write data into using a winapi function, and I want to get it in a std::wstring
, I know std::wstring t(wText);
works but I can't have it declared in an an if statement because it wouldn't exist outside that scope. Also I can't call the constructor in this fashion:
WCHAR wText[256];
std::wstring t = L"";
if (somecondition)
{
t = t(wText);
}
What can I do? I want the wstring
to be equal to ""
when the condition is not met and when it is met I want to get a WCHAR array into it.
From char* to wstring : char* str = "hello worlddd"; wstring wstr (str, str+strlen(str)); From string to wstring : string str = "hello worlddd"; wstring wstr (str.
This LPCWSTR is Microsoft defined. So to use them we have to include Windows. h header file into our program. To convert std::wstring to wide character array type string, we can use the function called c_str() to make it C like string and point to wide character string.
std::wstring str1 = L"SomeText"; std::wstring strr2(L"OtherText!"); printf("Wide String1- %ls \n", str1. c_str()); wprintf(L"Wide String2- %s \n", str2. c_str()); For printf: %s is narrow char string and %ls is wide char string.
This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function.
You can construct a temporary std::wstring
object by calling the constructor in the expression like so:
t = std::wstring(wText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With