Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mbsrtowcs

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

                             constchar** src,
                             std::size_t len,

                             std::mbstate_t* ps);

      Converts a null-terminated multibyte character sequence, which begins in the conversion state described by*ps, from the array whose first element is pointed to by*src to its wide character representation. Ifdst is not null, converted characters are stored in the successive elements of the wchar_t array pointed to bydst. No more thanlen wide characters are written to the destination array.

      Each multibyte character is converted as if by a call tostd::mbrtowc. The conversion stops if:

      • The multibyte null character was converted and stored.src is set to a null pointer and*ps represents the initial shift state.
      • An invalid multibyte character (according to the current C locale) was encountered.src is set to point at the beginning of the first unconverted multibyte character.
      • The next wide character to be stored would exceedlen.src is set to point at the beginning of the first unconverted multibyte character. This condition is not checked ifdst is a null pointer.

      Contents

      [edit]Parameters

      dst - pointer to wide character array where the results will be stored
      src - pointer to pointer to the first element of a null-terminated multibyte string
      len - number of wide characters available in the array pointed to by dst
      ps - pointer to the conversion state object

      [edit]Return value

      On success, returns the number of wide characters, excluding the terminatingL'\0', written to the character array. Ifdst is a null pointer, returns the number of wide characters that would have been written given unlimited length.

      On conversion error (if invalid multibyte character was encountered), returnsstatic_cast<std::size_t>(-1), storesEILSEQ inerrno, and leaves*ps in unspecified state.

      [edit]Notes

      This function moves thesrc pointer to the end of the converted multibyte string. This doesn't happen ifdst is a null pointer.

      [edit]Example

      Run this code
      #include <clocale>#include <cwchar>#include <iostream>#include <vector> void print_as_wide(constchar* mbstr){std::mbstate_t state=std::mbstate_t();std::size_t len=1+ std::mbsrtowcs(nullptr,&mbstr,0,&state);std::vector<wchar_t> wstr(len);    std::mbsrtowcs(&wstr[0],&mbstr, wstr.size(),&state);std::wcout<<"Wide string: "<<&wstr[0]<<'\n'<<"The length, including '\\0': "<< wstr.size()<<'\n';} int main(){std::setlocale(LC_ALL,"en_US.utf8");constchar* mbstr="z\u00df\u6c34\U0001f34c";// or u8"zß水🍌"    print_as_wide(mbstr);}

      Output:

      Wide string: zß水🍌The length, including '\0': 5

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp