Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      wctomb, wctomb_s

      From cppreference.com
      <c‎ |string‎ |multibyte
       
       
       
       
      Defined in header<stdlib.h>
      int wctomb(char*s,wchar_t wc);
      (1)
      errno_t wctomb_s(int*restrict status,char*restrict s, rsize_t ssz,wchar_t wc);
      (2)(since C11)
      1) Converts a wide characterwc to multibyte encoding and stores it (including any shift sequences) in the char array whose first element is pointed to bys. No more thanMB_CUR_MAX characters are stored. The conversion is affected by the current locale's LC_CTYPE category.
      Ifwc is the null character, the null byte is written tos, preceded by any shift sequences necessary to restore the initial shift state.
      Ifs is a null pointer, this function resets the global conversion state and determines whether shift sequences are used.
      2) Same as(1), except that the result is returned in the out-parameterstatus and the following errors are detected at runtime and call the currently installedconstraint handler function:
      • ssz is less than the number of bytes that would be written (unlesss is null)
      • ssz is greater thanRSIZE_MAX (unlesss is null)
      • s is a null pointer butssz is not zero
      As with all bounds-checked functions,wctomb_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

      Each call towctomb updates the internal global conversion state (a static object of typembstate_t, known only to this function). If the multibyte encoding uses shift states, this function is not reentrant. In any case, multiple threads should not callwctomb without synchronization:wcrtomb orwctomb_s may be used instead.

      Unlike most bounds-checked functions,wctomb_s does not null-terminate its output, because it is designed to be used in loops that process strings character-by-character.

      [edit]Parameters

      s - pointer to the character array for output
      wc - wide character to convert
      ssz - maximum number of bytes to write tos (size of the arrays)
      status - pointer to an out-parameter where the result (length of the multibyte sequence or the shift sequence status) will be stored

      [edit]Return value

      1) Ifs is not a null pointer, returns the number of bytes that are contained in the multibyte representation ofwc or-1 ifwc is not a valid character.
      Ifs is a null pointer, resets its internal conversion state to represent the initial shift state and returns0 if the current multibyte encoding is not state-dependent (does not use shift sequences) or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).
      2) zero on success, in which case the multibyte representation ofwc is stored ins and its length is stored in*status, or, ifs is null, the shift sequence status is stored instatus). Non-zero on encoding error or runtime constraint violation, in which case(size_t)-1 is stored in*status. The value stored in*status never exceedsMB_CUR_MAX

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h>#include <locale.h> void demo(wchar_t wc){constchar* dep= wctomb(NULL, wc)?"Yes":"No";printf("State-dependent encoding? %s.\n", dep); char mb[MB_CUR_MAX];int len= wctomb(mb, wc);printf("wide char '%lc' -> multibyte char [", wc);for(int idx=0; idx< len;++idx)printf("%s%#2x", idx?" ":"",(unsignedchar)mb[idx]);printf("]\n");} int main(void){setlocale(LC_ALL,"en_US.utf8");printf("MB_CUR_MAX = %zu\n", MB_CUR_MAX);    demo(L'A');    demo(L'\u00df');    demo(L'\U0001d10b');}

      Possible output:

      MB_CUR_MAX = 6State-dependent encoding? No.wide char 'A' -> multibyte char [0x41]State-dependent encoding? No.wide char 'ß' -> multibyte char [0xc3 0x9f]State-dependent encoding? No.wide char '𝄋' -> multibyte char [0xf0 0x9d 0x84 0x8b]

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.22.7.3 The wctomb function (p: 261)
      • K.3.6.4.1 The wctomb_s function (p: 443)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.22.7.3 The wctomb function (p: 358-359)
      • K.3.6.4.1 The wctomb_s function (p: 610-611)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.20.7.3 The wctomb function (p: 322-323)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.10.7.3 The wctomb function

      [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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/multibyte/wctomb&oldid=133917"

      [8]ページ先頭

      ©2009-2025 Movatter.jp