mbtowc, _mbtowc_l

Convierte un carácter multibyte en el carácter ancho correspondiente.

Sintaxis

int mbtowc(
   wchar_t *wchar,
   const char *mbchar,
   size_t count
);
int _mbtowc_l(
   wchar_t *wchar,
   const char *mbchar,
   size_t count,
   _locale_t locale
);

Parámetros

wchar
Dirección de un carácter ancho (tipo wchar_t).

mbchar
Dirección de una secuencia de bytes (un carácter multibyte).

count
Número de bytes que se va a comprobar.

locale
Configuración regional que se va a usar.

Valor devuelto

Si mbchar no NULLes , y si mbchar apunta a un carácter multibyte válido, mbtowc devuelve la longitud en bytes del carácter multibyte. Si mbchar es NULL o apunta a un carácter NULO de caracteres anchos (L'\0'), la función devuelve 0. Si el objeto al que mbchar apunta no forma un carácter multibyte válido dentro de los primeros count caracteres, devuelve -1.

Comentarios

La mbtowc función convierte count o menos bytes a mbcharlos que apunta , si mbchar no NULLes , a un carácter ancho correspondiente. mbtowc almacena el carácter ancho resultante en wchar, si wchar no NULLes . mbtowc no examina más de MB_CUR_MAX bytes. mbtowc usa la configuración regional actual para cualquier comportamiento dependiente de la configuración regional; _mbtowc_l es igual, salvo que en su lugar usa la configuración regional pasada. Para obtener más información, vea Locale.

De manera predeterminada, el estado global de esta función está limitado a la aplicación. Para cambiar este comportamiento, consulte Estado global en CRT.

Requisitos

Routine Encabezado necesario
mbtowc <stdlib.h>
_mbtowc_l <stdlib.h>

Para obtener más información sobre compatibilidad, consulte Compatibilidad.

Bibliotecas

Todas las versiones de las bibliotecas en tiempo de ejecución de C.

Ejemplo

// crt_mbtowc.c
// Illustrates the behavior of the mbtowc function

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

int main( void )
{
    int      i;
    char    *pmbc    = (char *)malloc( sizeof( char ) );
    wchar_t  wc      = L'a';
    wchar_t *pwcnull = NULL;
    wchar_t *pwc     = (wchar_t *)malloc( sizeof( wchar_t ) );
    printf( "Convert a wide character to multibyte character:\n" );
    wctomb_s( &i, pmbc, sizeof(char), wc );
    printf( "  Characters converted: %u\n", i );
    printf( "  Multibyte character: %x\n\n", *pmbc );

    printf( "Convert multibyte character back to a wide "
            "character:\n" );
    i = mbtowc( pwc, pmbc, MB_CUR_MAX );
    printf( "   Bytes converted: %u\n", i );
    printf( "   Wide character: %x\n\n", *pwc );
    printf( "Attempt to convert when target is NULL\n" );
    printf( "   returns the length of the multibyte character:\n" );
    i = mbtowc( pwcnull, pmbc, MB_CUR_MAX );
    printf( "   Length of multibyte character: %u\n\n", i );

    printf( "Attempt to convert a NULL pointer to a" );
    printf( " wide character:\n" );
    pmbc = NULL;
    i = mbtowc( pwc, pmbc, MB_CUR_MAX );
    printf( "   Bytes converted: %u\n", i );
}
Convert a wide character to multibyte character:
   Characters converted: 1
   Multibyte character: 61

Convert multibyte character back to a wide character:
   Bytes converted: 1
   Wide character: 61

Attempt to convert when target is NULL
   returns the length of the multibyte character:
   Length of multibyte character: 1

Attempt to convert a NULL pointer to a wide character:
   Bytes converted: 0

Consulte también

Conversión de datos
MultiByteToWideChar
Configuración regional
Interpretación de secuencias de caracteres multibyte
_mbclen, mblen, _mblen_l
wcstombs, _wcstombs_l
wctomb, _wctomb_l