std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::to_bytes - cppreference.com (original) (raw)

| Defined in header | | | | -------------------------------------------------------------------------- | --- | | | byte_string to_bytes( Elem wchar ); | (1) | | | byte_string to_bytes( const Elem* wptr ); | (2) | | | byte_string to_bytes( const wide_string& wstr ); | (3) | | | byte_string to_bytes( const Elem* first, const Elem* last ); | (4) | |

Converts a wide sequence to a byte string using the facet pointed to by _[cvtptr](../wstring%5Fconvert.html#cvtptr "cpp/locale/wstring convert")_ .

  1. The wide sequence only consists of one element byte.

  2. The wide sequence is the null-terminated sequence beginning at ptr.

  3. The wide sequence is the sequence contained in str.

  4. The wide sequence is the range [first, last).

Before the conversion begins, if *this was not constructed with constructor overload (3), _[cvtstate](../wstring%5Fconvert.html#cvtstate "cpp/locale/wstring convert")_ will be set to its default value (the initial conversion state).

The number of input elements successfully converted will be stored in _[cvtcount](../wstring%5Fconvert.html#cvtcount "cpp/locale/wstring convert")_ .

[edit] Return value

If the conversion succeeds, returns the conversion result. Otherwise, if *this is constructed with constructor overload (4), returns _[byteerrstring](../wstring%5Fconvert.html#byte%5Ferr%5Fstring "cpp/locale/wstring convert")_.

[edit] Exceptions

If the conversion fails and *this was not constructed with constructor overload (4), throws std::range_error.

[edit] Example

Output:

UTF-8 conversion produced 10 bytes: 7a c3 9f e6 b0 b4 f0 9f 8d 8c UTF-16le conversion produced 10 bytes: 7a 00 df 00 34 6c 3c d8 4c df

[edit] See also

| | converts a byte string into a wide string (public member function) [edit] | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | converts a wide string to narrow multibyte character string, given state (function) [edit] | | | converts a string from InternT to ExternT, such as when writing to file (virtual protected member function of std::codecvt<InternT,ExternT,StateT>) [edit] |