C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

How To Convert u32string To A wstring In C++

While C++ is one of the most modern programming languages in the world, in 2024, it still has problems with strings and conversions. One of the problems is new std::basic_string types, it is hard to display and convert them. For example, there are no perfect conversion methods between basic_strings, especially, from higher character type strings to lower character type strings, such as conversion from std::u32string to a std::wstring. In this post, we will explain these string types and how you can convert from a u32string to a wstring.

What is a wstring (std::wstring) in C++ ?

Wide strings are the string class for ‘wide’ characters represented by wstring. Alphanumeric characters are stored and displayed in string form. In other words wstring stores alphanumeric text with 2 or 4 byte chars. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. The type definition of a wstring is as given below:

What is u32string (std::u32string)?

The u32string (std::u32string or std::pmr::u32string) is the string class data type for the 32-bit characters defined in the std and std::pmr namespaces. It is a string class for 32-bit characters.

This instantiates the basic_string class template that uses char16_t as the character type, with its default char_traits and allocator types. For example, the std::string uses one byte (8 bits) while the std::u32string uses four bytes (32 bits) per each character of the text string. In basic string definition, std::u32string is defined as std::basic_string<char32_t>. Type definition can be shown as below,

Note that we released about conversion from u16string to wstring as discussed below.

Now, let’s see how we can convert u32string to a wstring.

Is there a simple example of how to use u32string and wstring in C++?

Here is a simple example of how to use std::u32string and std::wstring in C++.

How can we convert u32string to a wstring in C++?

First of all, we should say that we do not recommend converting the higher-byte string to a lower-byte string. Let’s assume that a developer wants to extract ASCII characters from u32string, and they don’t care about other character losses. In this situation, maybe this developer needs to convert u32string to wstring or later to a string or to a char array.

Simply we can convert u32string to a wstring as below ( Thank you Remy Lebeau ),

Or, we can create our u32string_to_wstring() method as described below.

We can use this conversion function like so:

the output for both options will be:

How can we correctly convert u32string to a wstring in C++?

There is another way to convert them by using std::wstring_convert and std::codecvt_utf16 templates. We can use the std::wstring_convert class template to convert a u16string to a wstring. The wstring_convert class template converts byte strings to wide strings using an individual code conversion tool, Codecvt These standard tools are suitable for use with the std::wstring_convert. We can use these with:

  • std::codecvt_utf8 for the UTF-8/UCS2 and UTF-8/UCS4 conversions
  • std::codecvt_utf8_utf16 for the UTF-8/UTF-16 conversions.

Note that codecvt_utf16 and wstring_convert are deprecated in C++17 and removed in C++26 and there is no better solution for Windows since they are not removed in most C++ compilers which means we can use them before C++26.

Syntax of std::wstring_convert class template (deprecated in C++17),

Syntax of std::codecvt_utf8 class template (deprecated in C++17):

In other way, we can use the following code (thank you Remy Lebeau ).

In my tests, std::wstring(str.begin(), str.end()) runs well on Windows with bcc32, bcc64, and with the latest Windows Modern 64-bit bcc64x compilers. But #ifdef MSWINDOWS ... #else part is recommended for some better conversions on Windows. Please check which one is suitable for your compiler.

How can we convert wstring to u32string in C++?

If you are looking conversion from wstring to u32string, here is how we can assign it in two different ways,

Is there a full example of how to convert u32string to a wstring in C++?

Here is a full example that converts a u32string to a wstring,

Here is the output.

How To Convert u32string To A wstring In C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

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++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

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

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?