summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fmultibyte%2Fmbsrtowcs.html
blob: 8154ac0acbcb2c87f90d318bd2f5025cdc51c630 (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
    <h1 id="firstHeading" class="firstHeading">mbsrtowcs, mbsrtowcs_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 t-until-c99"> <td><pre data-language="c">size_t mbsrtowcs( wchar_t* dst, const char** src, size_t len, mbstate_t* ps );</pre></td>  <td>
<span class="t-mark-rev t-since-c95">(since C95)</span> <br><span class="t-mark-rev t-until-c99">(until C99)</span>
</td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">size_t mbsrtowcs( wchar_t *restrict dst, const char **restrict src, size_t len,
                  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 mbsrtowcs_s( size_t *restrict retval,
                     wchar_t *restrict dst, rsize_t dstsz,
                     const char **restrict src, rsize_t len,
                     mbstate_t *restrict ps );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <span class="t-li">1)</span> Converts a null-terminated multibyte character sequence, which begins in the conversion state described by <code>*ps</code>, from the array whose first element is pointed to by <code>*src</code> to its wide character representation. If <code>dst</code> is not null, converted characters are stored in the successive elements of the wchar_t array pointed to by <code>dst</code>. No more than <code>len</code> wide characters are written to the destination array. Each multibyte character is converted as if by a call to <code><a href="mbrtowc" title="c/string/multibyte/mbrtowc">mbrtowc</a></code>. The conversion stops if: <ul>
<li> The multibyte null character was converted and stored. <code>*src</code> is set to null pointer value and <code>*ps</code> represents the initial shift state. </li>
<li> An invalid multibyte character (according to the current C locale) was encountered. <code>*src</code> is set to point at the beginning of the first unconverted multibyte character. </li>
<li> the next wide character to be stored would exceed <code>len</code>. <code>*src</code> is set to point at the beginning of the first unconverted multibyte character. This condition is not checked if <code>dst</code> is a null pointer.</li>
</ul> <span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that <ul>
<li> the function returns its result as an out-parameter <code>retval</code> </li>
<li> if no null character was written to <code>dst</code> after <code>len</code> wide characters were written, then <code>L'\0'</code> is stored in <code>dst[len]</code>, which means len+1 total wide characters are written </li>
<li> the function clobbers the destination array from the terminating null and until <code>dstsz</code> </li>
<li> If <code>src</code> and <code>dst</code> overlap, the behavior is unspecified. </li>
<li> 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: </li>
</ul> <dl>
<dd>
<ul>
<li> <code>retval</code>, <code>ps</code>, <code>src</code>, or <code>*src</code> is a null pointer </li>
<li> <code>dstsz</code> or <code>len</code> is greater than <code>RSIZE_MAX/sizeof(wchar_t)</code> (unless <code>dst</code> is null) </li>
<li> <code>dstsz</code> is not zero (unless <code>dst</code> is null) </li>
<li> There is no null character in the first <code>dstsz</code> multibyte characters in the <code>*src</code> array and <code>len</code> is greater than <code>dstsz</code> (unless <code>dst</code> is null) </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>mbsrtowcs_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>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dst </td> <td> - </td> <td> pointer to wide character array where the results will be stored </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to pointer to the first element of a null-terminated multibyte string </td>
</tr> <tr class="t-par"> <td> len </td> <td> - </td> <td> number of wide characters available in the array pointed to by dst </td>
</tr> <tr class="t-par"> <td> ps </td> <td> - </td> <td> pointer to the conversion state object </td>
</tr> <tr class="t-par"> <td> dstsz </td> <td> - </td> <td> max number of wide characters that will be written (size of the <code>dst</code> array) </td>
</tr> <tr class="t-par"> <td> retval </td> <td> - </td> <td> pointer to a size_t object where the result 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 wide characters, excluding the terminating <code>L'\0'</code>, written to the character array. If <code>dst</code> is a null pointer, returns the number of wide characters that would have been written given unlimited length. On conversion error (if invalid multibyte character was encountered), 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> zero on success (in which case the number of wide characters excluding terminating zero that were, or would be written to <code>dst</code>, is stored in <code>*retval</code>), non-sero on error. In case of a runtime constraint violation, stores <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> in <code>*retval</code> (unless <code>retval</code> is null) and sets <code>dst[0]</code> to <code>L'\0'</code> (unless <code>dst</code> is null or <code>dstmax</code> is zero or greater than <code>RSIZE_MAX</code>)</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;wchar.h&gt;
#include &lt;string.h&gt;
 
void print_as_wide(const char* mbstr)
{
    mbstate_t state;
    memset(&amp;state, 0, sizeof state);
    size_t len = 1 + mbsrtowcs(NULL, &amp;mbstr, 0, &amp;state);
    wchar_t wstr[len];
    mbsrtowcs(&amp;wstr[0], &amp;mbstr, len, &amp;state);
    wprintf(L"Wide string: %ls \n", wstr);
    wprintf(L"The length, including L'\\0': %zu\n", len);
}
 
int main(void)
{
    setlocale(LC_ALL, "en_US.utf8");
    print_as_wide(u8"z\u00df\u6c34\U0001f34c"); // u8"zß水🍌"
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">Wide string: zß水🍌
The length, including L'\0': 5</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.6.4.1 The mbsrtowcs function (p: 445) </li>
<li> K.3.9.3.2.1 The mbsrtowcs_s function (p: 648-649) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.24.6.4.1 The mbsrtowcs function (p: 391) </li></ul>
</ul>       <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="mbstowcs" title="c/string/multibyte/mbstowcs"> <span class="t-lines"><span>mbstowcs</span><span>mbstowcs_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 narrow multibyte character string to wide string <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> <div><a href="wcsrtombs" title="c/string/multibyte/wcsrtombs"> <span class="t-lines"><span>wcsrtombs</span><span>wcsrtombs_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a wide string to narrow multibyte character string, 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/mbsrtowcs" title="cpp/string/multibyte/mbsrtowcs">C++ documentation</a></span> for <code>mbsrtowcs</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/mbsrtowcs" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/mbsrtowcs</a>
  </p>
</div>