# codecvt

***

**1. Converting between wide and narrow character strings**

```cpp
#include <codecvt>

void convert_wide_to_narrow() {
  std::wstring wstr = L"Hello, world!";  // Wide-character string
  std::codecvt_utf8<wchar_t> utf8_conv;  // UTF-8 codecvt facet
  std::mbstate_t mbs;  // Multibyte state

  // Convert the wide-character string to a narrow-character string
  size_t size = utf8_conv.max_length(wstr.size());  // Get the maximum number of bytes required
  char *str = new char[size];  // Allocate memory for the narrow-character string
  auto res = utf8_conv.out(mbs, wstr.data(), wstr.data() + wstr.size(), str, str + size, mbs);

  if (res == std::codecvt_base::ok) {
    // Conversion successful
    std::cout << str << std::endl;  // Output the narrow-character string
  } else {
    // Conversion failed
    std::cerr << "Conversion failed" << std::endl;
  }

  delete[] str;  // Free the memory allocated for the narrow-character string
}

void convert_narrow_to_wide() {
  std::string str = "Hello, world!";  // Narrow-character string
  std::codecvt_utf8<wchar_t> utf8_conv;  // UTF-8 codecvt facet
  std::mbstate_t mbs;  // Multibyte state

  // Convert the narrow-character string to a wide-character string
  size_t size = utf8_conv.max_length(str.size());  // Get the maximum number of wide characters required
  wchar_t *wstr = new wchar_t[size];  // Allocate memory for the wide-character string
  auto res = utf8_conv.in(mbs, str.data(), str.data() + str.size(), wstr, wstr + size, mbs);

  if (res == std::codecvt_base::ok) {
    // Conversion successful
    std::wcout << wstr << std::endl;  // Output the wide-character string
  } else {
    // Conversion failed
    std::cerr << "Conversion failed" << std::endl;
  }

  delete[] wstr;  // Free the memory allocated for the wide-character string
}
```

**2. Converting between UTF-8 and UTF-16**

```cpp
#include <codecvt>

void convert_utf8_to_utf16() {
  std::string utf8_str = "Hello, world!";  // UTF-8 encoded string
  std::wstring utf16_str;  // UTF-16 encoded string

  // Convert the UTF-8 encoded string to a UTF-16 encoded string
  std::codecvt_utf8_utf16<wchar_t> utf8_utf16_conv;  // UTF-8 to UTF-16 codecvt facet
  std::mbstate_t mbs;  // Multibyte state

  utf8_utf16_conv.unshift(mbs);  // Reset the conversion state
  utf16_str.resize(utf8_utf16_conv.max_length(utf8_str.size()));  // Resize the UTF-16 encoded string to the maximum possible length

  auto res = utf8_utf16_conv.in(mbs, utf8_str.data(), utf8_str.data() + utf8_str.size(), utf16_str.data(), utf16_str.data() + utf16_str.size() - 1, mbs);

  if (res != std::codecvt_base::ok || utf16_str.back() != '\0') {
    // Conversion failed or the UTF-16 encoded string is not null-terminated
    std::cerr << "Conversion failed" << std::endl;
  } else {
    // Conversion successful
    utf16_str.resize(utf16_str.size() - 1);  // Remove the null-terminator from the UTF-16 encoded string
    std::wcout << utf16_str << std::endl;  // Output the UTF-16 encoded string
  }
}

void convert_utf16_to_utf8() {
  std::wstring utf16_str = L"Hello, world!";  // UTF-16 encoded string
  std::string utf8_str;  // UTF-8 encoded string

  // Convert the UTF-16 encoded string to a UTF-8 encoded string
  std::codecvt_utf8_utf16<wchar_t> utf16_utf8_conv;  // UTF-16 to UTF-8 codecvt facet
  std::mbstate_t mbs;  // Multibyte state

  utf16_utf8_conv.unshift(mbs);  // Reset the conversion state
  utf8_str.resize(utf16_utf8_conv.max_length(utf16_str.size()));  // Resize the UTF-8 encoded string to the maximum possible length

  auto res = utf16_utf8_conv.out(mbs, utf16_str.data(), utf16_str.data() + utf16_str.size(), utf8_str.data(), utf8_str.data() + utf8_str.size() - 1, mbs);

  if (res != std::codecvt_base::ok || utf8_str.back() != '\0') {
    // Conversion failed or the UTF-8 encoded string is not null-terminated
    std::cerr << "Conversion failed" << std::endl;
  } else {
    // Conversion successful
    utf8_str.resize(utf8_str.size() - 1);  // Remove the null-terminator from the UTF-8 encoded string
    std::cout << utf8_str << std::endl;  // Output the UTF-8 encoded string
  }
}
```

**3. Converting between UTF-32 and UTF-8**

```cpp
#include <codecvt>

void convert_utf32_to_utf8() {
  std::u32string utf32_str = U"Hello, world!";  // UTF-32 encoded string
  std::string utf8_str;  // UTF-8 encoded string

  // Convert the UTF-32 encoded string to a UTF-8 encoded string
  std::codecvt_utf8<char32_t> utf32_utf8_conv;  // UTF-32 to UTF-8 codecvt facet
  std::mbstate_t mbs;  // Multibyte state

  utf32_utf8_conv.unshift(mbs);  // Reset the conversion state
  utf8_str.resize(utf32_utf8_conv.max_length(utf32_str.size()));  // Resize the UTF-8 encoded string to the maximum possible length

  auto res = utf32_utf8_conv.out(mbs, utf32_str.data(), utf32_str.data() + utf32_str.size(), utf8_str.data(), utf8_str.data() + utf8_str.size() - 1, mbs);

  if (res != std::codecvt_base::ok || utf8_str.back() != '\0') {
    // Conversion failed or the UTF-8 encoded string is not null-terminated
    std::cerr << "Conversion failed" << std::endl;
  } else {
    // Conversion successful
    utf8_str.resize(utf8_str.size() - 1);  // Remove the null-terminator from the UTF-8 encoded string
    std::cout << utf8_str << std::endl;  // Output the UTF-8 encoded string
  }
}

void convert_utf8_to_utf32() {
  std::string utf8_str = "Hello, world!";  // UTF-8 encoded string
  std::u32string utf32_str;  // UTF-32 encoded string

  // Convert the UTF-8 encoded string to a UTF-32 encoded string
  std::codecvt_utf8<char32_t> utf8_utf32_conv;  // UTF-8 to UTF-32 codecvt facet
  std::mbstate_t mbs

```
