Site Tools


wiki:hpp-codecvt

Table of Contents

<codecvt>

<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.

Example

This example uses a codecvt facet to convert a UTF-8 string to a wide character string via a wstring_convert wrapper.

// 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;
}
wiki/hpp-codecvt.md · Last modified: by 127.0.0.1