| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wide/multibyte conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C95) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Macros | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <wchar.h> | ||
| (1) | ||
| (since C95) | ||
| (since C99) | ||
| (2) | (since C11) | |
Converts a wide character to its narrow multibyte representation.
s is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation ofwc (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.s is a null pointer, the call is equivalent towcrtomb(buf, L'\0', ps) for some internal bufferbuf.s is a null pointer, the call is equivalent towcrtomb_s(&retval, buf,sizeof buf, L'\0', ps) with internal variablesretval andbuf (whose size is greater thanMB_CUR_MAX)retvalretval orps is a null pointer.ssz is zero or greater thanRSIZE_MAX (unlesss is null)ssz is less than the number of bytes that would be written (unlesss is null)s is a null pointer butssz is not zerowcrtomb_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<wchar.h>.Contents |
| s | - | pointer to narrow character array where the multibyte character will be stored |
| wc | - | the wide character to convert |
| ps | - | pointer to the conversion state object used when interpreting the multibyte string |
| ssz | - | max number of bytes to write (the size of the buffers) |
| retval | - | pointer to an out-parameter where the result (number of bytes in the multibyte string including any shift sequences) will be stored |
s.s is null orssz is zero or greater thanRSIZE_MAX) and*retval is set to(size_t)-1 (unlessretval is null)#include <stdio.h>#include <locale.h>#include <string.h>#include <wchar.h>#include <stdlib.h> int main(void){setlocale(LC_ALL,"en_US.utf8");mbstate_t state;memset(&state,0,sizeof state);wchar_t in[]= L"zß水🍌";// or "z\u00df\u6c34\U0001F34C"size_t in_sz=sizeof in/sizeof*in; printf("Processing %zu wchar_t units: [ ", in_sz);for(size_t n=0; n< in_sz;++n)printf("%#x ",(unsignedint)in[n]);puts("]"); char out[MB_CUR_MAX* in_sz];char*p= out;for(size_t n=0; n< in_sz;++n){int rc= wcrtomb(p, in[n],&state);if(rc==-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 ",+(unsignedchar)out[x]);puts("]");}
Output:
Processing 5 wchar_t units: [ 0x7a 0xdf 0x6c34 0x1f34c 0 ]into 11 UTF-8 code units: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c 0 ]
(C11) | converts a wide character to its multibyte representation (function)[edit] |
(C95) | converts the next multibyte character to wide character, given state (function)[edit] |
C++ documentation forwcrtomb | |