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
|
<h1 id="firstHeading" class="firstHeading">c32rtomb</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><uchar.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">size_t c32rtomb( char* restrict s, char32_t c32, mbstate_t* restrict ps );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Converts a single code point from its variable-length 32-bit wide character representation (but typically, UTF-32) to its narrow multibyte character representation.</p>
<p>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>c32</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.</p>
<p>If <code>s</code> is a null pointer, the call is equivalent to <code>c32rtomb(buf, U'\0', ps)</code> for some internal buffer <code>buf</code>.</p>
<p>If <code>c32</code> is the null wide character <code>U'\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.</p>
<p>If the macro <code>__STDC_UTF_32__</code> is defined, the 32-bit encoding used by this function is UTF-32; otherwise, it is implementation-defined. <span class="t-rev-inl t-since-c23"><span>The macro is always defined and the encoding is always UTF-32.</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> In any case, the multibyte character encoding used by this function is specified by the currently active C locale.</p>
<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> c32 </td> <td> - </td> <td> the 32-bit 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>
</table> <h3 id="Return_value"> Return value</h3> <p>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>. This value may be <code>0</code>, e.g. when processing the leading <code>char32_t</code> units in a multi-<code>char32_t</code>-unit sequence (does not occur in UTF-32).</p>
<p>On failure (if <code>c32</code> is not a valid 32-bit wide character), returns <code>-1</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.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <uchar.h>
mbstate_t state;
int main(void)
{
setlocale(LC_ALL, "en_US.utf8");
const char32_t in[] = U"zß水🍌"; // or "z\u00df\u6c34\U0001F34C"
size_t in_sz = sizeof in / sizeof *in;
printf("Processing %zu UTF-32 code units: [ ", in_sz);
for (size_t n = 0; n < in_sz; ++n)
printf("%#x ", in[n]);
puts("]");
char out[MB_CUR_MAX * in_sz];
char* p = out;
for (size_t n = 0; n < in_sz; ++n)
{
size_t rc = c32rtomb(p, in[n], &state);
if(rc == (size_t)-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 < 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 UTF-32 code 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> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.30.1.6 The c32rtomb function (p: 411) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.28.1.4 The c32rtomb function (p: 401) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="mbrtoc32" title="c/string/multibyte/mbrtoc32"> <span class="t-lines"><span>mbrtoc32</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> generates the next 32-bit wide character from a narrow multibyte 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/c32rtomb" title="cpp/string/multibyte/c32rtomb">C++ documentation</a></span> for <code>c32rtomb</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/c32rtomb" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/c32rtomb</a>
</p>
</div>
|