Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::to_bytes

      From cppreference.com
      <cpp‎ |locale‎ |wstring convert
       
       
       
      Localization library
       
       
      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 .

      1) The wide sequence only consists of one elementbyte.
      2) The wide sequence is the null-terminated sequence beginning atptr.
      3) The wide sequence is the sequence contained instr.
      4) The wide sequence is the range[firstlast).

      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

      [edit]Return value

      If the conversion succeeds, returns the conversion result. Otherwise, if*this is constructed with constructor overload(4), returnsbyte_err_string.

      [edit]Exceptions

      If the conversion fails and*this wasnot constructed with constructor overload(4), throwsstd::range_error.

      [edit]Example

      Run this code
      #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

      [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]
      [virtual]
      converts a string fromInternT toExternT, such as when writing to file
      (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/wstring_convert/to_bytes&oldid=177202"

      [8]ページ先頭

      ©2009-2026 Movatter.jp