RAD Studio |  |

mbtowc

Header File

stdlib.h

Category

Conversion Routines, Memory and String Manipulation Routines

Prototype

int mbtowc(wchar_t *pwc, const char *s, size_t n);

Description

Converts a multibyte character to wchar_t code.

If s is not null, mbtowc determines the number of bytes that comprise the multibyte character pointed to by s. Next, mbtowc determines the value of the type wchar_t that corresponds to that multibyte character. If there is a successful match between wchar_t and the multibyte character, and pwc is not null, the wchar_t value is stored in the array pointed to by pwc. At most n characters are examined.

Return Value

When s points to an invalid multibyte character, -1 is returned. When s points to the null character, 0 is returned. Otherwise, mbtowc returns the number of bytes that comprise the converted multibyte character.

The return value never exceeds MB_CUR_MAX or the value of n.

Example

Copy Code

    #include <stdlib.h>
    #include<stdio.h>
    void main(void)
    {
      int     x;
      char     *mbchar    = (char *)calloc(1, sizeof( char));
      wchar_t  wchar      = L'a';
      wchar_t   *pwcnull = NULL;
      wchar_t  *pwchar    = (wchar_t *)calloc(1,  sizeof( wchar_t ));
      printf ("Convert a wide character to multibyte character:\n");
      x = wctomb( mbchar, wchar);
      printf( "\tCharacters converted: %u\n", x);
      printf( "\tMultibyte character: %x\n\n", mbchar);
      printf ("Convert multibyte character back to a wide character:\n");
      x = mbtowc( pwchar, mbchar, MB_CUR_MAX );
      printf( "\tBytes converted: %u\n", x);
      printf( "\tWide character: %x\n\n", pwchar);
      printf ("Atempt to convert when target is NULL\n" );
      printf (" returns the length of the multibyte character:\n" );
      x = mbtowc (pwcnull, mbchar, MB_CUR_MAX );
      printf ( "\tlength of multibyte character:%u\n\n", x );
      printf ("Attempt to convert a NULL pointer to a" );
      printf (" wide character:\n" );
      mbchar = NULL;
      x = mbtowc (pwchar, mbchar, MB_CUR_MAX);
      printf( "\tBytes converted: %u\n", x );
      }

Portability

POSIX | Win32 | ANSI C | ANSI C++
+ | + | + | +

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!