mbtowc, _mbtowc_l

Convertir un caractère multioctet en un caractère large correspondant.

Syntaxe

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
);

Paramètres

wchar
Adresse d’un caractère large (type wchar_t).

mbchar
Adresse d'une séquence d'octets (un caractère multioctet).

count
Nombre d'octets à vérifier.

locale
Paramètres régionaux à utiliser.

Valeur retournée

Si mbchar ce n’est pas NULLle cas, et si mbchar pointe vers un caractère multioctet valide, mbtowc retourne la longueur en octets du caractère multioctet. Si mbchar elle pointe NULL vers un caractère null à caractère large (L'\0'), la fonction retourne 0. Si l’objet qui mbchar pointe vers ne forme pas de caractère multioctet valide dans les premiers count caractères, il retourne -1.

Notes

La mbtowc fonction convertit ou moins d’octets count pointés par mbchar, si mbchar ce n’est pas NULLle cas, en un caractère large correspondant. mbtowc stocke le caractère large obtenu à wchar, si wchar ce n’est pas le cas NULL. mbtowc n’examine pas plus d’octets MB_CUR_MAX . La fonction mbtowc utilise les paramètres régionaux actuels pour le comportement dépendant des paramètres régionaux ; la fonction _mbtowc_l est identique, à ceci près qu’elle utilise à la place les paramètres régionaux qui ont été passés. Pour plus d’informations, consultez Locale.

Par défaut, l’état global de cette fonction est limité à l’application. Pour modifier ce comportement, consultez État global dans le CRT.

Spécifications

Routine En-tête requis
mbtowc <stdlib.h>
_mbtowc_l <stdlib.h>

Pour plus d’informations sur la compatibilité, consultez Compatibility.

Bibliothèques

Toutes les versions des bibliothèques Runtime C.

Exemple

// 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

Voir aussi

Conversion de données
MultiByteToWideChar
Paramètres régionaux
Interprétation des séquences de caractères multioctets
_mbclen, mblen, _mblen_l
wcstombs, _wcstombs_l
wctomb, _wctomb_l