mbtowc_mbtowc_l

将多字节字符转换为相应的宽字符。

语法

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

参数

wchar
宽字符的地址(类型 wchar_t)。

mbchar
字节(多字节字符)序列的地址。

count
要检查的字节数。

locale
要使用的区域设置。

返回值

如果 mbchar 不是 NULL,并且如果 mbchar 指向有效的多字节字符,则 mbtowc 会返回该多字节字符的长度(以字节为单位)。 如果 mbcharNULL 或指向宽字符空字符 (L'\0'),则该函数会返回 0。 如果 mbchar 指向的对象未构成前 count 个字符内的有效多字节字符,则它会返回 -1。

备注

如果 mbchar 不是 NULL,则 mbtowc 函数会将 mbchar 指向的 count 个或更少的字节转换为相应的宽字符。 mbtowc 将生成的宽字符存储在 wchar 中(如果 wchar 不是 NULL)。 mbtowc 检查的字节数不超过 MB_CUR_MAXmbtowc 将当前区域设置用于与区域设置相关的行为;_mbtowc_l 也是一样,只不过它使用传入的区域设置。 有关详细信息,请参阅 Locale

默认情况下,此函数的全局状态范围限定为应用程序。 要更改此行为,请参阅 CRT 中的全局状态

要求

例程 必需的标头
mbtowc <stdlib.h>
_mbtowc_l <stdlib.h>

有关兼容性的详细信息,请参阅 兼容性

C 运行时库的所有版本。

示例

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

另请参阅

数据转换
MultiByteToWideChar
区域设置
多字节字符序列的解释
_mbclenmblen_mblen_l
wcstombs_wcstombs_l
wctomb_wctomb_l