Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mbrtoc16

      From cppreference.com
      <cpp‎ |string‎ |multibyte
       
       
       
       
      Defined in header<cuchar>
      std::size_t mbrtoc16(char16_t* pc16,constchar* s,
                           std::size_t n,std::mbstate_t* ps);
      (since C++11)

      Converts a narrow multibyte character to UTF-16 character representation.

      Ifs is not a null pointer, inspects at mostn bytes of the multibyte character string, beginning with the byte pointed to bys to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character ins is complete and valid, converts it to the corresponding 16-bit character and stores it in*pc16 (ifpc16 is not null).

      If the multibyte character in*s corresponds to a multi-char16_t sequence (e.g., a surrogate pair in UTF-16), then after the first call to this function,*ps is updated in such a way that the next call tombrtoc16 will write out the additionalchar16_t, without considering*s.

      Ifs is a null pointer, the values ofn andpc16 are ignored and the call is equivalent tostd::mbrtoc16(nullptr,"",1, ps).

      If the wide character produced is the null character, the conversion state*ps represents the initial shift state.

      The multibyte encoding used by this function is specified by the currently active C locale.

      Contents

      [edit]Parameters

      pc16 - pointer to the location where the resulting 16-bit character will be written
      s - pointer to the multibyte character string used as input
      n - limit on the number of bytes in s that can be examined
      ps - pointer to the conversion state object used when interpreting the multibyte string

      [edit]Return value

      The first of the following that applies:

      • 0 if the character converted froms (and stored in*pc16 if non-null) was the null character.
      • the number of bytes[1n] of the multibyte character successfully converted froms.
      • -3 if the nextchar16_t from a multi-char16_t character (e.g., a surrogate pair) has now been written to*pc16. No bytes are processed from the input in this case.
      • -2 if the nextn bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to*pc16.
      • -1 if encoding error occurs. Nothing is written to*pc16, the valueEILSEQ is stored inerrno and the value of*ps is unspecified.

      [edit]Example

      Run this code
      #include <clocale>#include <cstring>#include <cuchar>#include <cwchar>#include <iomanip>#include <iostream> int main(){std::setlocale(LC_ALL,"en_US.utf8"); std::string str{"z\u00df\u6c34\U0001F34C"};// or u8"zß水🍌" std::cout<<"Processing "<< str.size()<<" bytes: ["<<std::uppercase<<std::setfill('0')<<std::hex;for(int n{};unsignedchar c: str)std::cout<<(n++?" ":"")<<+c;std::cout<<"]\n"; std::mbstate_t state{};// zero-initialized to initial statechar16_t c16{};constchar* ptr{&str[0]},*end{&str[0]+ str.size()}; while(std::size_t rc{std::mbrtoc16(&c16, ptr, end- ptr+1,&state)}){std::cout<<"Next UTF-16 char: "<<std::setw(4)<<static_cast<unsignedshort>(c16)<<" obtained from ";if(rc==std::size_t(-3))std::cout<<"earlier surrogate pair\n";elseif(rc==std::size_t(-2))continue;elseif(rc==std::size_t(-1))break;else{std::cout<<std::dec<< rc<<" bytes [";for(std::size_t n{}; n!= rc;++n)std::cout<<(n?" ":"")<<std::hex<<+static_cast<unsignedchar>(ptr[n]);std::cout<<"]\n";            ptr+= rc;}}}

      Output:

      Processing 10 bytes: [7A C3 9F E6 B0 B4 F0 9F 8D 8C]Next UTF-16 char: 007A obtained from 1 bytes [7A]Next UTF-16 char: 00DF obtained from 2 bytes [C3 9F]Next UTF-16 char: 6C34 obtained from 3 bytes [E6 B0 B4]Next UTF-16 char: D83C obtained from 4 bytes [F0 9F 8D 8C]Next UTF-16 char: DF4C obtained from earlier surrogate pair

      [edit]See also

      (C++11)
      converts a UTF-16 character to narrow multibyte encoding
      (function)[edit]
      (C++20)
      converts a narrow multibyte character to UTF-8 encoding
      (function)[edit]
      [virtual]
      converts a string fromExternT toInternT, such as when reading from file
      (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>)[edit]
      C documentation formbrtoc16
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/multibyte/mbrtoc16&oldid=182478"

      [8]ページ先頭

      ©2009-2025 Movatter.jp