Format
#include <wchar.h> int wcrtomb(char *dest, wchar_t wc, mbstate_t *ps);
Language Level: ANSI 93
wcrtomb is a restartable version of wctomb, and performs the same
function. It first determines the length of the wide character
pointed to by wc. It then converts the wide character
to the corresponding multibyte character, and stores the
converted character in the location pointed to by dest,
if dest is not a null pointer. A maximum of MB_CUR_MAX
bytes are stored.
With wcrtomb, you can switch from one multibyte string to another. On systems that support shift states, ps represents the initial shift state of the string (0). If you read in only part of the string, wcrtomb sets ps to the string's shift state at the point you stopped. You can then call wcrtomb again for that string and pass in the updated ps value to continue reading where you left off.
Because both OS/2 and Windows do not have shift states, the ps parameter is provided only for compatibility with other ANSI/ISO platforms. This product ignores the value passed for ps.
The behavior of wcrtomb is affected by the LC_CTYPE category of the current locale.
Return Value
If dest is a null pointer, wcrtomb returns 0.
If dest is not a null pointer, wcrtomb returns the
number of bytes stored in dest. If wc is
not a valid wide character, wcrtomb sets errno to EILSEQ and
returns -1.
Example
This example uses wcrtomb to convert two wide-character
strings to multibyte strings.
#include <stdlib.h> #include <stdio.h> #include <locale.h> #include <wchar.h>
#define SIZE 20 #define LOCNAME "ja_jp.ibm-932"
int main(void)
{
wchar_t wcs1[] = L"abc";
wchar_t wcs2[] = L"Ax8142" L"C";
mbstate_t ss1 = 0;
mbstate_t ss2 = 0;
size_t length1 = 0;
size_t length2 = 0;
char mb1[SIZE], mb2[SIZE];
int i;
if (NULL == setlocale(LC_ALL, LOCNAME)) {
printf("Locale \"%s\" could not be loaded\n", LOCNAME);
exit(1);
}
for (i = 0; i <=2; ++i) {
length1 += wcrtomb(mb1 + length1, *(wcs1 + i), &ss1);
length2 += wcrtomb(mb2 + length2, *(wcs2 + i), &ss2);
}
mb1[length1] = 0;
mb2[length2] = 0;
printf("The first multibyte string is: <%s>\n", mb1);
printf("The second multibyte string is: <%s>\n", mb2);
return 0;
/********************************************************
The output should be similiar to:
The first multibyte string is: <abc>
The second multibyte string is: <A BC>
********************************************************/
}
![]()
mblen -- Determine Length of Multibyte
Character
mbrlen -- Calculate Length of Multibyte
Character
mbrtowc -- Convert Multibyte Character to
Wide Character
mbsrtowcs -- Convert Multibyte String to
Wide-Character String
wcsrtombs -- Convert Wide-Character String
to Multibyte String
wctomb -- Convert Wide Character to
Multibyte Character
wchar.h