skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-codecvt
This page is read only. You can view the source, but not change it.
# <codecvt> **[<codecvt>](https://en.cppreference.com/w/cpp/header/codecvt)** provides locale-based character encoding conversion facets, primarily for converting between UTF-8, UTF-16, and UTF-32. It's part of the locales system, which is complex and largely superseded by explicit UTF-8 handling in modern C++. Most new code avoids this in favor of dedicated UTF-8 libraries or C++20 `<text_encoding>`. It's mainly present for compatibility with legacy code. This example uses a codecvt facet to convert a UTF-8 string to a wide character string via a `wstring_convert` wrapper. ```cpp // compile: g++ -std=c++11 -o codecvtexample codecvtexample.cpp // run: ./codecvtexample // description: UTF-8 to UTF-32 conversion via codecvt #include <codecvt> #include <locale> #include <iostream> #include <string> int main() { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; std::string utf8_str = "hello"; std::wstring wide = converter.from_bytes(utf8_str); std::cout << "converted: " << wide.size() << " wide chars\n"; return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top