Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mbstowcs

      From cppreference.com
      <cpp‎ |string‎ |multibyte
       
       
       
       
      Defined in header<cstdlib>
      std::size_t mbstowcs(wchar_t* dst,constchar* src,std::size_t len);

      Converts a multibyte character string from the array whose first element is pointed to bysrc to its wide character representation. Converted characters are stored in the successive elements of the array pointed to bydst. No more thanlen wide characters are written to the destination array.

      Each character is converted as if by a call tostd::mbtowc, except that the mbtowc conversion state is unaffected. The conversion stops if:

      • The multibyte null character was converted and stored.
      • An invalid (in the current C locale) multibyte character was encountered.
      • The next wide character to be stored would exceedlen.

      Contents

      [edit]Notes

      In most implementations, this function updates a global static object of typestd::mbstate_t as it processes through the string, and cannot be called simultaneously by two threads,std::mbsrtowcs should be used in such cases.

      POSIX specifies a common extension: ifdst is a null pointer, this function returns the number of wide characters that would be written todst, if converted. Similar behavior is standard forstd::mbsrtowcs.

      [edit]Parameters

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

      [edit]Return value

      On success, returns the number of wide characters, excluding the terminatingL'\0', written to the destination array.

      On conversion error (if invalid multibyte character was encountered), returnsstatic_cast<std::size_t>(-1).

      [edit]Example

      Run this code
      #include <clocale>#include <cstdlib>#include <iostream> int main(){std::setlocale(LC_ALL,"en_US.utf8");std::wcout.imbue(std::locale("en_US.utf8"));constchar* mbstr="z\u00df\u6c34\U0001f34c";// or u8"zß水🍌"// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";wchar_t wstr[5];    std::mbstowcs(wstr, mbstr,5);std::wcout<<"wide string: "<< wstr<<'\n';}

      Output:

      wide string: zß水🍌

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp