template < class Elem, unsigned long MaxCode = 0x10ffffUL, codecvt_mode Mode = (codecvt_mode)0 >
class codecvt_utf8_utf16 : public codecvt <Elem, char, mbstate_t>
Convert between UTF-8 and UTF-16
Converts between multibyte sequences encoded in UTF-8 and UTF-16.
The facet uses Elem as its internal character type (encoded as UTF-16), and char as its external character type (encoded as UTF-8). Therefore:
- The internal character type, aliased as member internal_type. This shall be a wide character type: wchar_t, char16_t or char32_t.
For 32bit-wide characters, conversions in of characters result in one UTF-16 code unit stored per wide character (as a 32-bit value).
The external character type in this facet is always char.
- The largest code point that will be translated without reporting a conversion error.
- Bitmask value of type codecvt_mode:
|consume_header||4||An optional initial header sequence (BOM) is read to determine whether a multibyte sequence converted in is big-endian or little-endian.|
|generate_header||2||An initial header sequence (BOM) shall be generated to indicate whether a multibyte sequence converted out is big-endian or little-endian.|
|little_endian||1||The multibyte sequence generated on conversions out shall be little-endian (as opposed to the default big-endian).|
The following aliases are member types of codecvt_utf8_utf16, inherited from codecvt:
|intern_type||The first template parameter (Elem)||The internal character type (encoded as UTF-16).|
|extern_type||char||The external character type (encoded as UTF-8).|
|state_type||mbstate_t||Conversion state type (see mbstate_t).|
|result||codecvt::result||Enum type with the result of a conversion operation (see codecvt::result).|
Public member functions inherited from codecvt
- codecvt constructor (public member function)
- Translate in characters (public member function)
- Translate out characters (public member function)
- Unshift translation state (public member function)
Character encoding properties:
- Return noconv characteristics (public member function)
- Return encoding width (public member function)
- Return length of translated sequence (public member function)
- Return max length of one character (public member function)
Virtual protected member functions
The class defines its functionality through its virtual protected member functions:
// codecvt_utf8_utf16 example
int main ()
std::string mbs = conversion.to_bytes( u"\u4f60\u597d" ); // ni hao (你好)
// print out hex value of each byte:
std::cout << std::hex;
for (int i=0; i<mbs.length(); ++i)
std::cout << int(unsigned char(mbs[i])) << ' ';
std::cout << '\n';