Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mbrtowc

      From cppreference.com
      <cpp‎ |string‎ |multibyte
       
       
       
       
      Defined in header<cwchar>
      std::size_t mbrtowc(wchar_t* pwc,

                           constchar* s,
                           std::size_t n,

                           std::mbstate_t* ps);

      Converts a narrow multibyte character to a wide character.

      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 wide character and stores it in*pwc (ifpwc is not null).

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

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

      Contents

      [edit]Parameters

      pwc - pointer to the location where the resulting wide 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 used when interpreting the multibyte string

      [edit]Return value

      The first of the following that applies:

      • 0 if the character converted froms (and stored inpwc if non-null) was the null character.
      • the number of bytes[1...n] of the multibyte character successfully converted froms.
      • static_cast<std::size_t>(-2) if the nextn bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to*pwc.
      • static_cast<std::size_t>(-1) if encoding error occurs. Nothing is written to*pwc, the valueEILSEQ is stored inerrno and the value of*ps is left unspecified.

      [edit]Example

      Run this code
      #include <clocale>#include <cstring>#include <cwchar>#include <iostream> void print_mb(constchar* ptr){std::mbstate_t state=std::mbstate_t();// initial stateconstchar* end= ptr+std::strlen(ptr);int len;wchar_t wc;while((len= std::mbrtowc(&wc, ptr, end-ptr,&state))>0){std::wcout<<"Next "<< len<<" bytes are the character "<< wc<<'\n';        ptr+= len;}} int main(){std::setlocale(LC_ALL,"en_US.utf8");// UTF-8 narrow multibyte encodingconstchar* str="z\u00df\u6c34\U0001d10b";// or u8"zß水𝄋"// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";    print_mb(str);}

      Output:

      Next 1 bytes are the character zNext 2 bytes are the character ßNext 3 bytes are the character 水Next 4 bytes are the character 𝄋

      [edit]See also

      converts the next multibyte character to wide character
      (function)[edit]
      converts a wide character to its multibyte representation, given state
      (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 formbrtowc
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/multibyte/mbrtowc&oldid=153082"

      [8]ページ先頭

      ©2009-2025 Movatter.jp