C++C++11C++14C++17Introduction to C++Learn C++

How To Convert A String to a Wide String In A Modern C++ App

How can I convert std::string to a std::wstring in a modern C++ app? How can I convert single byte string to 2-bytes wide string? Here are the answers with C++ examples.

In Modern C++, there are several typedefs for common character types are provided: Wide string types are defined in header <string>.

strings are the string class for byte characters represented with string. Alphanumeric characters are normally stored and displayed in string forms. The string class stores the alphanumeric text with 1-byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,

wstrings (Wide Strings) are the string class for characters that has 2 bytes that represented with wstring. In Modern C++, alphanumeric characters are stored in wide strings because of their supports to characters of world languages and displayed in wstring forms. In another terms wstring stores for the alphanumeric text with 2 byte chars, called wchar_t or WChar. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a wstring as below,

The string class has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can insert a wide string into another wide string in different methods. Let’s see some of most used methods.

Converting a String to a Wide String in a C++ app

We can convert string (std::string) to a wstring (std::wstring) easily. To do this we should create a new wstring by using begin() and end() iterators of that string. Thus, this wstring will have same characters in that given range. See example below,

and the Wide String output will be,

that means we successfully copied a string to a wide string.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Inserting A String Into A Wide String In A Modern C++ App

C++C++11C++14C++17Introduction to C++Learn C++

How To Use WString find_first_of() In A Modern C++ App

C++Learn C++

How To Read An XML File In A C Program Or C++ App

C++Learn C++

Solve A 3 Diagonal Matrix Using Successive Over Relaxation