diff options
Diffstat (limited to 'devdocs/c/string%2Fmultibyte%2Fwcstombs.html')
| -rw-r--r-- | devdocs/c/string%2Fmultibyte%2Fwcstombs.html | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fmultibyte%2Fwcstombs.html b/devdocs/c/string%2Fmultibyte%2Fwcstombs.html new file mode 100644 index 00000000..ae8b6652 --- /dev/null +++ b/devdocs/c/string%2Fmultibyte%2Fwcstombs.html @@ -0,0 +1,104 @@ + <h1 id="firstHeading" class="firstHeading">wcstombs, wcstombs_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 wcstombs( char *dst, const wchar_t *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 wcstombs( char *restrict dst, const wchar_t *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 wcstombs_s( size_t *restrict retval, char *restrict dst, rsize_t dstsz, + const wchar_t *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 sequence of wide characters from the array whose first element is pointed to by <code>src</code> to its narrow multibyte representation that begins in the initial shift state. Converted characters are stored in the successive elements of the char array pointed to by <code>dst</code>. No more than <code>len</code> bytes are written to the destination array.</div> <div class="t-li1"> + Each character is converted as if by a call to <code><a href="wctomb" title="c/string/multibyte/wctomb">wctomb</a></code>, except that the wctomb's conversion state is unaffected. The conversion stops if:</div> <div class="t-li1"> + * The null character <code>L'\0'</code> was converted and stored. The bytes stored in this case are the unshift sequence (if necessary) followed by <code>'\0'</code>,</div> <div class="t-li1"> + * A <code>wchar_t</code> was found that does not correspond to a valid character in the current C locale.</div> <div class="t-li1"> + * The next multibyte 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 unspecified.</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="wcrtomb" title="c/string/multibyte/wcrtomb">wcrtomb</a></code>, not <code><a href="wctomb" title="c/string/multibyte/wctomb">wctomb</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 the conversion stops without writing a null character, the function will store <code>'\0'</code> in the next byte in <code>dst</code>, which may be <code>dst[len]</code> or <code>dst[dstsz]</code>, whichever comes first (meaning up to len+1/dstsz+1 total bytes may be written). In this case, there may be no unshift sequence written before the terminating null.</div> <div class="t-li1"> + * if <code>dst</code> is a null pointer, the number of bytes 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</code> (unless <code>dst</code> is null) </li> +<li> <code>dstsz</code> is not zero (unless <code>dst</code> is null) </li> +<li> <code>len</code> is greater than <code>dstsz</code> and the conversion does not encounter null or encoding error in the <code>src</code> array by the time <code>dstsz</code> is reached (unless <code>dst</code> is null) </li> +</ul> </dd> +<dd>As with all bounds-checked functions, <code>wcstombs_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>wcstombs</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="wcsrtombs" title="c/string/multibyte/wcsrtombs">wcsrtombs</a></code> or <code>wcstombs_s</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 bytes that would be written to <code>dst</code>, if converted. Similar behavior is standard for <code><a href="wcsrtombs" title="c/string/multibyte/wcsrtombs">wcsrtombs</a></code> and <code>wcstombs_s</code>.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dst </td> <td> - </td> <td> pointer to narrow character array where the multibyte character will be stored </td> +</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the first element of a null-terminated wide string to convert </td> +</tr> <tr class="t-par"> <td> len </td> <td> - </td> <td> number of bytes available in the array pointed to by dst </td> +</tr> <tr class="t-par"> <td> dstsz </td> <td> - </td> <td> max number of bytes 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 bytes (including any shift sequences, but excluding the terminating <code>'\0'</code>) written to the character array whose first element is pointed to by <code>dst</code>. On conversion error (if invalid wide 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> Returns zero on success (in which case the number of bytes 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>'\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 <stdlib.h> +#include <locale.h> + +int main(void) +{ + // 4 wide characters + const wchar_t src[] = L"z\u00df\u6c34\U0001f34c"; + // they occupy 10 bytes in UTF-8 + char dst[11]; + + setlocale(LC_ALL, "en_US.utf8"); + printf("wide-character string: '%ls'\n",src); + for (size_t ndx=0; ndx < sizeof src/sizeof src[0]; ++ndx) + printf(" src[%2zu] = %#8x\n", ndx, src[ndx]); + + int rtn_val = wcstombs(dst, src, sizeof dst); + printf("rtn_val = %d\n", rtn_val); + if (rtn_val > 0) + printf("multibyte string: '%s'\n",dst); + for (size_t ndx=0; ndx<sizeof dst; ++ndx) + printf(" dst[%2zu] = %#2x\n", ndx, (unsigned char)dst[ndx]); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">wide-character string: 'zß水🍌' + src[ 0] = 0x7a + src[ 1] = 0xdf + src[ 2] = 0x6c34 + src[ 3] = 0x1f34c + src[ 4] = 0 +rtn_val = 10 +multibyte string: 'zß水🍌' + dst[ 0] = 0x7a + dst[ 1] = 0xc3 + dst[ 2] = 0x9f + dst[ 3] = 0xe6 + dst[ 4] = 0xb0 + dst[ 5] = 0xb4 + dst[ 6] = 0xf0 + dst[ 7] = 0x9f + dst[ 8] = 0x8d + dst[ 9] = 0x8c + dst[10] = 0</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.22.8.2 The wcstombs function (p: 360) </li> +<li> K.3.6.5.2 The wcstombs_s function (p: 612-614) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.20.8.2 The wcstombs function (p: 324) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.10.8.2 The wcstombs function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <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> <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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/multibyte/wcstombs" title="cpp/string/multibyte/wcstombs">C++ documentation</a></span> for <code>wcstombs</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/wcstombs" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/wcstombs</a> + </p> +</div> |
