std::codecvt_byname

From Cppreference

Jump to: navigation, search
Defined in header <locale>

template< class internT, class externT, class stateT >
class codecvt_byname;

std::codecvt_byname is a std::codecvt facet which encapsulates multibyte/wide character conversion rules of a locale specified at its construction.

Four specializations are provided by the standard library

Defined in header <locale>
std::codecvt_byname<char, char, std::mbstate_t> identity conversion
std::codecvt_byname<char16_t, char, std::mbstate_t> conversion between UTF-16 and UTF-8 (C++11 feature)
std::codecvt_byname<char32_t, char, std::mbstate_t> conversion between UTF-32 and UTF-8 (C++11 feature)
std::codecvt_byname<wchar_t, char, std::mbstate_t> locale-specific conversion between wide string and narrow, possibly multibyte, string

[edit] Member functions

(constructor)
constructs a new codecvt_byname facet
(public member function)
(destructor)
destructs a codecvt_byname facet
(protected member function)

[edit] Example

This example demonstrates reading a GB18030-encoded file using the codecvt facet from a GB18030-aware locale

#include <iostream>
#include <fstream>
#include <string>
#include <locale>
int main()
{
    // GB18030 narrow multibyte encoding
    std::ofstream("text.txt") << "\x7a"              // letter 'z', U+007a
                                 "\x81\x30\x89\x38"  // letter 'ß', U+00df
                                 "\xcb\xae"          // CJK ideogram '水' (water), U+6c34
                                 "\x94\x32\xbc\x35"; // musical sign '𝄋' (segno), U+1d10b
    std::wifstream fin("text.txt");
    fin.imbue(std::locale(fin.getloc(),
              new std::codecvt_byname<wchar_t, char, std::mbstate_t>("zh_CN.gb18030")));
    for(wchar_t c; fin.get(c); )
        std::cout << std::hex << std::showbase << c << '\n';
}

Output:

0x7a
0xdf
0x6c34
0x1d10b

[edit] See also

codecvt
converts between character encodings, including UTF-8, UTF-16, UTF-32
(class template)