VOOZH about

URL: https://www.geeksforgeeks.org/cpp/utf-8-to-wide-char-conversion-in-cpp-stl/

⇱ UTF-8 to Wide Char Conversion in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

UTF-8 to Wide Char Conversion in C++ STL

Last Updated : 23 Jul, 2025

UTF - 8 is a variable-length encoding that represents Unicode characters using 1 to 4 bytes. It’s widely used for text storage and transmission due to its compactness and compatibility with ASCII. Wide Characters (wchar_t) is a type that represents a single character in a wide character encoding (usually UTF-16 or UTF-32). The size of wchar_t varies across platforms (e.g., 2 bytes on Windows, 4 bytes on Unix-like systems).

In this article, we’ll explore how to convert between UTF-8 and wide character (wchar_t) strings using the C++ standard library.

Methods to Convert UTF-8 characters to Wide Char in C++

There are multiple methods to convert between UTF-8 and wide character (wchar_t) strings using the C++ standard library. Here are few of them:

1. Convert UTF-8 characters to Wide Char using std::wstring_convert

std::wstring_convert is part of the C++11 standard library, defined in the <codecvt> header. It's a template class that facilitates conversions between different character encodings.

Syntax to Create std::wstring_convert

wstring_convert<facet> converter;

where, facet is the codecvt facet for the conversion of the given type of character string to another. For UTF-8 to wchar conversion, it is: codecvt_utf8.

Afterwards, we can use this convertor to convert the given string as shown in the below

Example


Output
Converted wide string: Hello, ??

Time Complexity: O(n), where n is the number of characters in the string.
Space Complexity: O(1)

2. Convert UTF-8 characters to Wide Char Using std::mbstowcs

Thestd::mbstowcsfunction is used to convert a multibyte string to a wide character string. It is defined inside <cstdlib> header file.

Syntax

mbstowcs(dest, src, len);

where,

  • dest: destination string.
  • src: source string
  • len: length of the string to be converted.

But before using this function, we need to set the locale to a locale that supports UTF-8. We can do that using the following statement:

setlocale(LC_ALL, "");

Example


Output

Converted wide string: Hello, 世界

Time Complexity: O(n), where n is the number of characters in the string.
Space Complexity: O(1)

3. Convert UTF-8 characters to Wide Char Using MultiByteToWideChar on Windows

In C++, MultiByteToWideChar() is a Windows API function that converts a string from a multibyte character set to a wide character (Unicode) string. It's part of the Windows SDK defined inside windows.h header file.

Example


Output

Converted wide string: Hello, 世界

Time Complexity: O(n), where n is the number of characters in the string.
Space Complexity: O(1)

4. Convert UTF-8 characters to Wide Char Using iconv on Unix-like Systems

iconv is a standardized library for converting between character encodings. It's available on Unix-like systems under iconv.h header file.

Example

Output

Converted wide string: Hello, 世界

Time Complexity: O(n), where n is the number of characters in the string.
Space Complexity: O(1)








































































Comment
Article Tags:
Article Tags: