| Functions | |||||||||||||||||||||||||||||||||||||||||
| Wide/multibyte conversions | |||||||||||||||||||||||||||||||||||||||||
(C95) | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Types | |||||||||||||||||||||||||||||||||||||||||
| Macros | |||||||||||||||||||||||||||||||||||||||||
Defined in header <wchar.h> | ||
| (1) | ||
| (since C95) (until C99) | ||
| (since C99) | ||
| (2) | (since C11) | |
*ps, from the array whose first element is pointed to by*src to its wide character representation. Ifdst is not null, converted characters are stored in the successive elements of the wchar_t array pointed to bydst. No more thanlen wide characters are written to the destination array. Each multibyte character is converted as if by a call tombrtowc. The conversion stops if:*ps represents the initial shift state.len.*src is set to point at the beginning of the first unconverted multibyte character. This condition is not checked ifdst is a null pointer.retvaldst afterlen wide characters were written, thenL'\0' is stored indst[len], which means len+1 total wide characters are writtendstszsrc anddst overlap, the behavior is unspecified.retval,ps,src, or*src is a null pointerdstsz orlen is greater thanRSIZE_MAX/sizeof(wchar_t) (unlessdst is null)dstsz is not zero (unlessdst is null)dstsz multibyte characters in the*src array andlen is greater thandstsz (unlessdst is null)mbsrtowcs_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 wide character array where the results will be stored |
| src | - | pointer to pointer to the first element of a null-terminated multibyte string |
| len | - | number of wide characters available in the array pointed to by dst |
| ps | - | pointer to the conversion state object |
| dstsz | - | max number of wide characters that will be written (size of thedst array) |
| retval | - | pointer to a size_t object where the result will be stored |
dst is a null pointer, returns the number of wide characters that would have been written given unlimited length. On conversion error (if invalid multibyte character was encountered), returns(size_t)-1, storesEILSEQ inerrno, and leaves*ps in unspecified state.dst, is stored in*retval), non-sero on error. In case of a runtime constraint violation, stores(size_t)-1 in*retval (unlessretval is null) and setsdst[0] toL'\0' (unlessdst is null ordstmax is zero or greater thanRSIZE_MAX)#include <stdio.h>#include <locale.h>#include <wchar.h>#include <string.h> void print_as_wide(constchar* mbstr){mbstate_t state;memset(&state,0,sizeof state);size_t len=1+ mbsrtowcs(NULL,&mbstr,0,&state);wchar_t wstr[len]; mbsrtowcs(&wstr[0],&mbstr, len,&state);wprintf(L"Wide string: %ls\n", wstr);wprintf(L"The length, including L'\\0': %zu\n", len);} int main(void){setlocale(LC_ALL,"en_US.utf8"); print_as_wide(u8"z\u00df\u6c34\U0001f34c");// u8"zß水🍌"}
Output:
Wide string: zß水🍌The length, including L'\0': 5
(C11) | converts a narrow multibyte character string to wide string (function)[edit] |
(C95) | converts the next multibyte character to wide character, given state (function)[edit] |
(C95)(C11) | converts a wide string to narrow multibyte character string, given state (function)[edit] |
C++ documentation formbsrtowcs | |