Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between WCHAR* to wstring without use of constructor

Tags:

c++

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.

like image 382
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Oct 27 '14 17:10

ᴘᴀɴᴀʏɪᴏᴛɪs


People also ask

How do I assign a string to Wstring?

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.

How do you convert Wstring to Lpcwstr?

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.

How do I initialize Wstring?

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.

What is a Wstring C++?

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.


1 Answers

You can construct a temporary std::wstring object by calling the constructor in the expression like so:

t = std::wstring(wText);
like image 187
PeterT Avatar answered Sep 21 '22 15:09

PeterT