Chapter Contents

Previous

Next
wctomb

wctomb



Convert a Wide Character to a Multibyte Character

Portability: ISO/ANSI C conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTIONS
EXAMPLE


SYNOPSIS

#include <stdlib.h>

int wctomb(char *s, wchar_t wchar);


DESCRIPTION

wctomb determines how many bytes are needed to represent the multibyte character corresponding to the wide (pure DBCS) character whose value is wchar , including any change in shift state. It stores the multibyte character representation in the array pointed to by s , assuming s is not NULL . If the value of wchar is 0 , wctomb is left in the initial shift state.


RETURN VALUE

If s is not NULL , the return value is the number of bytes that make up the multibyte character corresponding to the value of wchar .

If s is NULL , the return value is as follows:
nonzero value is returned if the current locale supports state-dependent encodings.
0 is returned if the current locale does not support state-dependent encodings.

The return value is never greater than the value of the MB_CUR_MAX macro.


CAUTIONS

A diagnostic is not issued if wctomb encounters invalid data; a return value of -1 is the only indication of an error.


EXAMPLE

This example converts a PURE DBCS string to MIXED, stopping at the first new-line character. This example uses wctomb .

#include <stdlib.h>
#include <locale.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_CHARACTERS 81

   /* "pure_string" is the input PURE DBCS string.               */
   /* "mixed_string" the output MIXED DBCS string.               */
void mbline(wchar_t *pure_string, char *mixed_string)
{
   int i;
   int mbclen;
   wchar_t wc;
      /* Inform library that we will be accepting a DBCS string. */
      /* That is, SO and SI are not regular control characters:  */
      /* they indicate a change in shift state.                  */
   setlocale(LC_ALL, "dbcs");

   wctomb(NULL, 0);             /* Reset to initial shift state. */

      /* One loop iteration per character. Advance "mixed_string"*/
      /* by number of bytes in character.                        */
   i = 0;
   do {
      wc = pure_string[i++];
      mbclen = wctomb(mixed_string, wc);
      if (mbclen < 0) {
         puts("Invalid PURE DBCS string.\n");
         abort();
         fclose(stdout);
      }
      mixed_string += mbclen;
   } while (wc != L'\n');

   *mixed_string = '\0';
}


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.