summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fmultibyte%2Fwcrtomb.html
blob: 1addcb02b9bfcb00e2c219aa20cdd2517ae975af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    <h1 id="firstHeading" class="firstHeading">wcrtomb, wcrtomb_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;wchar.h&gt;</code> </th> <th> </th> <th> </th> </tr>  <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-since-c95"> <td> <pre data-language="c">size_t wcrtomb( char *s, wchar_t wc, mbstate_t *ps);</pre>
</td>  <td> <span class="t-mark-rev t-since-c95">(since C95)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">size_t wcrtomb( char *restrict s, wchar_t wc, mbstate_t *restrict ps);</pre>
</td>  <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t wcrtomb_s(size_t *restrict retval, char *restrict s, rsize_t ssz,
                  wchar_t wc, mbstate_t *restrict ps);</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <p>Converts a wide character to its narrow multibyte representation.</p>
<div class="t-li1">
<span class="t-li">1)</span> If <code>s</code> is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation of <code>wc</code> (including any shift sequences, and taking into account the current multibyte conversion state <code>*ps</code>), and stores the multibyte character representation in the character array whose first element is pointed to by <code>s</code>, updating <code>*ps</code> as necessary. At most <code>MB_CUR_MAX</code> bytes can be written by this function.</div> <div class="t-li1">
 If <code>s</code> is a null pointer, the call is equivalent to <code>wcrtomb(buf, L'\0', ps)</code> for some internal buffer <code>buf</code>.</div> <div class="t-li1">
 If wc is the null wide character <code>L'\0'</code>, a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter <code>*ps</code> is updated to represent the initial shift state.</div> <div class="t-li1">
 If the environment macro <code>__STDC_ISO_10646__</code> is defined, the values of type <code>wchar_t</code> are the same as the short identifiers of the characters in the Unicode required set (typically UTF-32 encoding); otherwise, it is implementation-defined. In any case, the multibyte character encoding used by this function is specified by the currently active C locale.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that</div> <div class="t-li1">
 if <code>s</code> is a null pointer, the call is equivalent to <code>wcrtomb_s(&amp;retval, buf, sizeof buf, L'\0', ps)</code> with internal variables <code>retval</code> and <code>buf</code> (whose size is greater than <code>MB_CUR_MAX</code>)</div> <div class="t-li1">
 the result is returned in the out-parameter <code>retval</code>
</div> <div class="t-li1">
 the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl>
<dd>
<ul>
<li> <code>retval</code> or <code>ps</code> is a null pointer. </li>
<li> <code>ssz</code> is zero or greater than <code>RSIZE_MAX</code> (unless <code>s</code> is null) </li>
<li> <code>ssz</code> is less than the number of bytes that would be written (unless <code>s</code> is null) </li>
<li> <code>s</code> is a null pointer but <code>ssz</code> is not zero </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>wcrtomb_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../wide" title="c/string/wide"><code>&lt;wchar.h&gt;</code></a>.</dd>
</dl>
</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> s </td> <td> - </td> <td> pointer to narrow character array where the multibyte character will be stored </td>
</tr> <tr class="t-par"> <td> wc </td> <td> - </td> <td> the wide character to convert </td>
</tr> <tr class="t-par"> <td> ps </td> <td> - </td> <td> pointer to the conversion state object used when interpreting the multibyte string </td>
</tr> <tr class="t-par"> <td> ssz </td> <td> - </td> <td> max number of bytes to write (the size of the buffer <code>s</code>) </td>
</tr> <tr class="t-par"> <td> retval </td> <td> - </td> <td> pointer to an out-parameter where the result (number of bytes in the multibyte string including any shift sequences) will be stored </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by <code>s</code>. </div> <div class="t-li1">
 On failure (if <code>wc</code> is not a valid wide character), returns <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">1</span></code>, stores <code><a href="../../error/errno_macros" title="c/error/errno macros">EILSEQ</a></code> in <code><a href="../../error/errno" title="c/error/errno">errno</a></code>, and leaves <code>*ps</code> in unspecified state.</div> <div class="t-li1">
<span class="t-li">2)</span> Returns zero on success and non-zero on failure, in which case, <code>s[0]</code> is set to <code>'\0'</code> (unless <code>s</code> is null or <code>ssz</code> is zero or greater than <code>RSIZE_MAX</code>) and <code>*retval</code> is set to <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">1</span></code> (unless <code>retval</code> is null)</div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;locale.h&gt;
#include &lt;string.h&gt;
#include &lt;wchar.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    setlocale(LC_ALL, "en_US.utf8");
    mbstate_t state;
    memset(&amp;state, 0, sizeof state);
    wchar_t in[] = L"zß水🍌"; // or "z\u00df\u6c34\U0001F34C"
    size_t in_sz = sizeof in / sizeof *in;
 
    printf("Processing %zu wchar_t units: [ ", in_sz);
    for(size_t n = 0; n &lt; in_sz; ++n) printf("%#x ", (unsigned int)in[n]);
    puts("]");
 
    char out[MB_CUR_MAX * in_sz];
    char *p = out;
    for(size_t n = 0; n &lt; in_sz; ++n) {
        int rc = wcrtomb(p, in[n], &amp;state); 
        if(rc == -1) break;
        p += rc;
    }
 
    size_t out_sz = p - out;
    printf("into %zu UTF-8 code units: [ ", out_sz);
    for(size_t x = 0; x &lt; out_sz; ++x) printf("%#x ", +(unsigned char)out[x]);
    puts("]");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">Processing 5 wchar_t units: [ 0x7a 0xdf 0x6c34 0x1f34c 0 ]
into 11 UTF-8 code units: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c 0 ]</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.6.3.3 The wcrtomb function (p: 444) </li>
<li> K.3.9.3.1.1 The wcrtomb_s function (p: 647-648) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.24.6.3.3 The wcrtomb function (p: 390) </li></ul>
</ul>       <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="wctomb" title="c/string/multibyte/wctomb"> <span class="t-lines"><span>wctomb</span><span>wctomb_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a wide character to its multibyte representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="mbrtowc" title="c/string/multibyte/mbrtowc"> <span class="t-lines"><span>mbrtowc</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> converts the next multibyte character to wide character, given state <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/multibyte/wcrtomb" title="cpp/string/multibyte/wcrtomb">C++ documentation</a></span> for <code>wcrtomb</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/string/multibyte/wcrtomb" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/wcrtomb</a>
  </p>
</div>