Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      mbstowcs, mbstowcs_s

      From cppreference.com
      <c‎ |string‎ |multibyte
       
       
       
       
      Defined in header<stdlib.h>
      (1)
      size_t mbstowcs(wchar_t          *dst,constchar          *src,size_t len)
      (until C99)
      size_t mbstowcs(wchar_t*restrict dst,constchar*restrict src,size_t len)
      (since C99)
      errno_t mbstowcs_s(size_t*restrict retval,wchar_t*restrict dst,
                        rsize_t dstsz,constchar*restrict src, rsize_t len);
      (2)(since C11)
      1) 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 tombtowc, 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.
      Ifsrc anddst overlap, the behavior is undefined
      2) Same as(1), except that
      * conversion is as-if bymbrtowc, notmbtowc
      * the function returns its result as an out-parameterretval
      * if no null character was written todst afterlen wide characters were written, thenL'\0' is stored indst[len], which means len+1 total wide characters are written
      * ifdst is a null pointer, the number of wide characters that would be produced is stored in*retval
      * the function clobbers the destination array from the terminating null and untildstsz
      * Ifsrc anddst overlap, the behavior is unspecified.
      * the following errors are detected at runtime and call the currently installedconstraint handler function:
      • retval orsrc is a null pointer
      • dstsz orlen is greater thanRSIZE_MAX/sizeof(wchar_t) (unlessdst is null)
      • dstsz is not zero (unlessdst is null)
      • There is no null character in the firstdstsz multibyte characters in thesrc array andlen is greater thandstsz (unlessdst is null)
      As with all bounds-checked functions,mbstowcs_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<stdlib.h>.

      Contents

      [edit]Notes

      In most implementations,mbstowcs updates a global static object of typembstate_t as it processes through the string, and cannot be called simultaneously by two threads,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 formbstowcs_s and formbsrtowcs.

      [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
      dstsz - max number of wide characters that will be written (size of thedst array)
      retval - pointer to a size_t object where the result will be stored

      [edit]Return value

      1) 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), returns(size_t)-1.
      2) zero on success (in which case the number of wide characters excluding terminating zero that were, or would be written todst, is stored in*retval), non-zero on error. In case of a runtime constraint violation, stores(size_t)-1 in*retval (unlessretval is null) and setsdst[0] toL'\0' (unlessdst is null ordstmax is zero or greater thanRSIZE_MAX)

      [edit]Example

      Run this code
      #include <stdio.h>#include <locale.h>#include <stdlib.h>#include <wchar.h> int main(void){setlocale(LC_ALL,"en_US.utf8");constchar* mbstr= u8"z\u00df\u6c34\U0001F34C";// or u8"zß水🍌"wchar_t wstr[5];    mbstowcs(wstr, mbstr,5);wprintf(L"MB string: %s\n", mbstr);wprintf(L"Wide string: %ls\n", wstr);}

      Output:

      MB string: zß水🍌Wide string: zß水🍌

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 7.22.8.1 The mbstowcs function (p: 359)
      • K.3.6.5.1 The mbstowcs_s function (p: 611-612)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.20.8.1 The mbstowcs function (p: 323)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.10.8.1 The mbstowcs function

      [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]
      C++ documentation formbstowcs
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/multibyte/mbstowcs&oldid=102011"

      [8]ページ先頭

      ©2009-2025 Movatter.jp