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.

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

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?