Javatpoint Logo
Javatpoint Logo

How to convert std::string to lpcwstr in C++?

In this article, we will discuss how to convert std::string to lpcwstr in C++ with its syntax and example.

Introduction:

A feature of C++ allows us to express a string of characters as an object belonging to a class. This class is the std::string. A string is internally represented by the std::string class as a series of characters (bytes), and the [] operator can be used to access any character in the string.

A string class is an element of the C++ library that provides us with the ability to create objects in which we may conveniently store string literals, whereas a character array is just an array of characters terminated with a null character. The following syntax can be used to define and initialize a string:

Syntax:

std::lpcwstr:

Long Pointer to Constant Wide STRing is referred to as LPCWSTR. It is a 32-bit pointer that can be null-terminated and points to a constant string of 16-bit Unicode letters. It is a string with wider characters to put it plainly.

Syntax:

It has the following syntax:

Programmers must include the <Window.h> header file to use LPCWSTR strings, which is defined by Microsoft. The main topic of this article is the conversion of std::string to LPCWSTR in C++ (Unicode).

In C++ converting a std::string to LPCWSTR

There are two steps involved in converting a string to LPCWSTR.

Step 1: String class object into a wstring

The initialization of the String class object into a wstring is the first step. Use std::wstring for wide-character/Unicode (UTF-16) strings. Simply feed the endpoint iterators of the supplied string to the std::wstring() initializer to do the transformation.

A wstring object is the result.

Step 2:

The returned wstring object (obtained from step 1) is then subjected to the c_str() method. The equivalent LPCWSTR string will be returned. The source code below serves as an example of the aforementioned procedure.

Example:

The C++ program to put the previously mentioned strategy into practise is shown below:

Output:

str is: javatpoint
wideString is: javatpoint






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA