Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::wcsrtombs

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

                             constwchar_t** src,
                             std::size_t len,

                             std::mbstate_t* ps);

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

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

      • The null character was converted and stored.src is set to a null pointer and*ps represents the initial shift state.
      • Awchar_t was found that does not correspond to a valid character in the current C locale.src is set to point at the first unconverted wide character.
      • The next multibyte character to be stored would exceedlen.src is set to point at the first unconverted wide character. This condition is not checked ifdst is a null pointer.

      Contents

      [edit]Parameters

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

      [edit]Return value

      On success, returns the number of bytes (including any shift sequences, but excluding the terminating'\0') written to the character array whose first element is pointed to bydst. Ifdst is a null pointer, returns the number of bytes that would have been written (again, excluding the terminating null character'\0').

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

      [edit]Example

      Run this code
      #include <clocale>#include <cwchar>#include <iostream>#include <string>#include <vector> void print_wide(constwchar_t* wstr){std::mbstate_t state=std::mbstate_t();std::size_t len=1+ std::wcsrtombs(nullptr,&wstr,0,&state);std::vector<char> mbstr(len);    std::wcsrtombs(&mbstr[0],&wstr, mbstr.size(),&state);std::cout<<"multibyte string: "<<&mbstr[0]<<'\n'<<"Length, including '\\0': "<< mbstr.size()<<'\n';} int main(){std::setlocale(LC_ALL,"en_US.utf8");// UTF-8 narrow multibyte encodingconstwchar_t* wstr= L"z\u00df\u6c34\U0001d10b";// or L"zß水𝄋"    print_wide(wstr);}

      Output:

      multibyte string: zß水𝄋Length, including '\0': 11

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp