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
|
<h1 id="firstHeading" class="firstHeading">mbstowcs, mbstowcs_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></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-until-c99"> <td> <pre data-language="c">size_t mbstowcs( wchar_t *dst, const char *src, size_t len)</pre>
</td> <td> <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 mbstowcs( wchar_t *restrict dst, const char *restrict src, size_t len)</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 mbstowcs_s(size_t *restrict retval, wchar_t *restrict dst,
rsize_t dstsz, const char *restrict src, rsize_t len);</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Converts a multibyte character string from the array whose first element is pointed to by <code>src</code> to its wide character representation. Converted characters are stored in the successive elements of the array pointed to by <code>dst</code>. No more than <code>len</code> wide characters are written to the destination array.</div> <div class="t-li1">
Each character is converted as if by a call to <code><a href="mbtowc" title="c/string/multibyte/mbtowc">mbtowc</a></code>, except that the mbtowc conversion state is unaffected. The conversion stops if:</div> <div class="t-li1">
* The multibyte null character was converted and stored.</div> <div class="t-li1">
* An invalid (in the current C locale) multibyte character was encountered.</div> <div class="t-li1">
* The next wide character to be stored would exceed <code>len</code>.</div> <div class="t-li1">
If <code>src</code> and <code>dst</code> overlap, the behavior is undefined</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">
* conversion is as-if by <code><a href="mbrtowc" title="c/string/multibyte/mbrtowc">mbrtowc</a></code>, not <code><a href="mbtowc" title="c/string/multibyte/mbtowc">mbtowc</a></code>
</div> <div class="t-li1">
* the function returns its result as an out-parameter <code>retval</code>
</div> <div class="t-li1">
* 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</div> <div class="t-li1">
* if <code>dst</code> is a null pointer, the number of wide characters that would be produced is stored in <code>*retval</code>
</div> <div class="t-li1">
* the function clobbers the destination array from the terminating null and until <code>dstsz</code>
</div> <div class="t-li1">
* If <code>src</code> and <code>dst</code> overlap, the behavior is unspecified.</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>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>mbstowcs_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="../../program" title="c/program"><code><stdlib.h></code></a>.</dd>
</dl>
</div> <h3 id="Notes"> Notes</h3> <p>In most implementations, <code>mbstowcs</code> updates a global static object of type <code><a href="mbstate_t" title="c/string/multibyte/mbstate t">mbstate_t</a></code> as it processes through the string, and cannot be called simultaneously by two threads, <code><a href="mbsrtowcs" title="c/string/multibyte/mbsrtowcs">mbsrtowcs</a></code> should be used in such cases.</p>
<p>POSIX specifies a common extension: if <code>dst</code> is a null pointer, this function returns the number of wide characters that would be written to <code>dst</code>, if converted. Similar behavior is standard for <code>mbstowcs_s</code> and for <code><a href="mbsrtowcs" title="c/string/multibyte/mbsrtowcs">mbsrtowcs</a></code>.</p>
<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 wide string will be stored </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the first element of a null-terminated multibyte string to convert </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> 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 destination array. 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>.</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-zero 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 <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <wchar.h>
int main(void)
{
setlocale(LC_ALL, "en_US.utf8");
const char* 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);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">MB string: zß水🍌
Wide string: zß水🍌</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.22.8.1 The mbstowcs function (p: 359) </li>
<li> K.3.6.5.1 The mbstowcs_s function (p: 611-612) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.8.1 The mbstowcs function (p: 323) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.8.1 The mbstowcs function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="mbsrtowcs" title="c/string/multibyte/mbsrtowcs"> <span class="t-lines"><span>mbsrtowcs</span><span>mbsrtowcs_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 narrow multibyte character string to wide string, given state <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="wcstombs" title="c/string/multibyte/wcstombs"> <span class="t-lines"><span>wcstombs</span><span>wcstombs_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 string to narrow multibyte character string <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/mbstowcs" title="cpp/string/multibyte/mbstowcs">C++ documentation</a></span> for <code>mbstowcs</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© 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/mbstowcs" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/mbstowcs</a>
</p>
</div>
|