| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wide/multibyte conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C95) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Macros | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <wchar.h> | ||
| (1) | ||
| (since C95) (until C99) | ||
| (since C99) | ||
| (2) | (since C11) | |
dst is not null, converted characters are stored in the successive elements of the char array pointed to bydst. No more thanlen bytes are written to the destination array. Each character is converted as if by a call towcrtomb. The conversion stops if:len.*src is set to point at the first unconverted wide character. This condition is not checked ifdst is a null pointer.retvaldst, which may bedst[len] ordst[dstsz], 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.dstszsrc anddst overlap, the behavior is unspecified.retval,ps,src, or*src is a null pointerdstsz orlen is greater thanRSIZE_MAX (unlessdst is null)dstsz is not zero (unlessdst is null)len is greater thandstsz and the conversion does not encounter null or encoding error in thesrc array by the timedstsz is reached (unlessdst is null)wcsrtombs_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 |
| dst | - | pointer to narrow character array where the multibyte characters will be stored |
| src | - | pointer to pointer to the first element of a null-terminated wide string |
| len | - | number of bytes available in the array pointed to by dst |
| ps | - | pointer to the conversion state object |
| dstsz | - | max number of bytes that will be written (size of thedst array) |
| retval | - | pointer to asize_t object where the result will be stored |
dst. Ifdst is a null pointer, returns the number of bytes that would have been written. On conversion error (if invalid wide character was encountered), returns(size_t)-1, storesEILSEQ inerrno, and leaves*ps in unspecified state.dst, is stored in*retval), non-zero on error. In case of a runtime constraint violation, stores(size_t)-1 in*retval (unlessretval is null) and setsdst[0] to'\0' (unlessdst is null ordstmax is zero or greater thanRSIZE_MAX)#include <stdio.h>#include <locale.h>#include <string.h>#include <wchar.h> void print_wide(constwchar_t* wstr){mbstate_t state;memset(&state,0,sizeof state);size_t len=1+ wcsrtombs(NULL,&wstr,0,&state);char mbstr[len]; wcsrtombs(mbstr,&wstr, len,&state);printf("Multibyte string: %s\n", mbstr);printf("Length, including '\\0': %zu\n", len);} int main(void){setlocale(LC_ALL,"en_US.utf8"); print_wide(L"z\u00df\u6c34\U0001f34c");// or L"zß水🍌"}
Output:
Multibyte string: zß水🍌Length, including '\0': 11
(C11) | converts a wide string to narrow multibyte character string (function)[edit] |
(C95)(C11) | converts a wide character to its multibyte representation, given state (function)[edit] |
(C95)(C11) | converts a narrow multibyte character string to wide string, given state (function)[edit] |
C++ documentation forwcsrtombs | |