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,
1 2 3 4 |
std::string str = "This is a String"; std::wstring wstr = L"This is a Wide String"; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { char s[] = "This is a Char Array"; std::wstring ws( &s[0], &s[strlen(s)] ); std::wcout << ws << std::endl; getchar(); return 0; } |
and the Wide String output will be,
1 2 3 |
This is a String |
that means we successfully copied a char array to a wide string.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition