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

Convert A Char Array To A Wide String In Modern C++ Software

Convert A Char Array To A Wide String In Modern C++ Software

How can I convert char array to a std::wstring? How can I convert chars to a 2-bytes wide string? Here are the answers with C++ examples.

Generally, as an introduction to C++, in addition to int, float, double there are other data types like string and wstring in modern C++ that we can use for alphanumeric variables. In Modern C++, there are several typedefs of common character types are provided: Wide string types are defined in header <string>.

char arrays are the easiest and oldest way to store strings in 1 byte ASCII chars. char arrays are located in a constant size, while there are methods to do dynamic allocations. That means generally their size is constant and that may cost a lot of character size in a lot of data when recording, manipulating, transferring all characters. They do not support Unicode characters which means there might be problems during

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other terms string stores for 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 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 string forms. In other 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,

string 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 convert a char array into a wide string with its constructor method. Let’s see how we do this;

Previous articles on converting std:string to a Char array in your C++ software

We’ve written before about the corresponding std:string methods to convert a std:string to a char array. You can find that post here:

Converting a Char Array to a Wide String

We can convert char array to a wide string (std::wstring) easily. To do this we should create a new wstring by pointing beginning address of char array ( &s[0] ) and ending address of char array ( &s[ strlen(s) ] ) iterators of that string. Thus, this new 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 char array 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
CC++Introduction to C++Language FeatureLearn C++

Who Invented The C++ Programming Language?

CC++C++17Introduction to C++Language FeatureLearn C++

What Is Count In C Programming?

CC++Introduction to C++Language FeatureLearn C++

Why Is C The Most Popular Programming Language?

CC++C++17Introduction to C++Learn C++Syntax

How To Use Switch In C++ And C Programming?