| Localization library | |||||||||||||||||||||||||
| Regular expressions library(C++11) | |||||||||||||||||||||||||
| Formatting library(C++20) | |||||||||||||||||||||||||
| Null-terminated sequence utilities | |||||||||||||||||||||||||
| Byte strings | |||||||||||||||||||||||||
| Multibyte strings | |||||||||||||||||||||||||
| Wide strings | |||||||||||||||||||||||||
| Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| Text encoding identifications | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
wstring_convert::to_bytes | ||||
Defined in header <locale> | ||
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 bycvtptr .
[first, last).Before the conversion begins, if*this wasnot constructed with constructor overload(3),cvtstate will be set to its default value (the initial conversion state).
The number of input elements successfully converted will be stored incvtcount .
Contents |
If the conversion succeeds, returns the conversion result. Otherwise, if*this is constructed with constructor overload(4), returnsbyte_err_string.
If the conversion fails and*this wasnot constructed with constructor overload(4), throwsstd::range_error.
#include <codecvt>#include <iomanip>#include <iostream>#include <locale>#include <string> // utility function for outputvoid hex_print(conststd::string& s){std::cout<<std::hex<<std::setfill('0');for(unsignedchar c: s)std::cout<<std::setw(2)<<static_cast<int>(c)<<' ';std::cout<<std::dec<<'\n';} int main(){// wide character datastd::wstring wstr= L"z\u00df\u6c34\U0001f34c";// or L"zß水🍌" // wide to UTF-8std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;std::string u8str= conv1.to_bytes(wstr);std::cout<<"UTF-8 conversion produced "<< u8str.size()<<" bytes:\n"; hex_print(u8str); // wide to UTF-16lestd::wstring_convert<std::codecvt_utf16<wchar_t,0x10ffff,std::little_endian>> conv2;std::string u16str= conv2.to_bytes(wstr);std::cout<<"UTF-16le conversion produced "<< u16str.size()<<" bytes:\n"; hex_print(u16str);}
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
| 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] | |
[virtual] | converts a string fromInternT toExternT, such as when writing to file(virtual protected member function of std::codecvt<InternT,ExternT,StateT>)[edit] |