| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wide/multibyte conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C95) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Macros | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <uchar.h> | ||
| (since C11) | ||
Converts a single code point from its variable-length 32-bit wide character representation (but typically, UTF-32) to its narrow multibyte character representation.
Ifs is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation ofc32 (including any shift sequences, and taking into account the current multibyte conversion state*ps), and stores the multibyte character representation in the character array whose first element is pointed to bys, updating*ps as necessary. At mostMB_CUR_MAX bytes can be written by this function.
Ifs is a null pointer, the call is equivalent toc32rtomb(buf, U'\0', ps) for some internal bufferbuf.
Ifc32 is the null wide characterU'\0', a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter*ps is updated to represent the initial shift state.
If the macro__STDC_UTF_32__ is defined, the 32-bit encoding used by this function is UTF-32; otherwise, it is implementation-defined.The macro is always defined and the encoding is always UTF-32.(since C23) In any case, the multibyte character encoding used by this function is specified by the currently active C locale.
Contents |
| s | - | pointer to narrow character array where the multibyte character will be stored |
| c32 | - | the 32-bit wide character to convert |
| ps | - | pointer to the conversion state object used when interpreting the multibyte string |
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to bys. This value may be0, e.g. when processing the leadingchar32_t units in a multi-char32_t-unit sequence (does not occur in UTF-32).
On failure (ifc32 is not a valid 32-bit wide character), returns-1, storesEILSEQ inerrno, and leaves*ps in unspecified state.
On MSVC you will need the/utf-8 compiler flag for UTF_8 to work properly.
#include <locale.h>#include <stdio.h>#include <stdlib.h>#include <uchar.h> 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("%s%08X", n?" ":"", in[n]);puts("]"); char* out=malloc(MB_CUR_MAX* in_sz);char* p= out;mbstate_t state={0}; 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("%s%02X", x?" ":"",+(unsignedchar)out[x]);puts("]"); free(out);}
Output:
Processing 5 UTF-32 code units: [0000007A 000000DF 00006C34 0001F34C 00000000]into 11 UTF-8 code units: [7A C3 9F E6 B0 B4 F0 9F 8D 8C 00]
(C11) | converts a narrow multibyte character to UTF-32 encoding (function)[edit] |
C++ documentation forc32rtomb | |