Share via


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 或 指向寬字元 Null 字元 (L'\0'),函式會傳回 0。 如果指向 的物件 mbchar 未在第一 count 個字元內形成有效的多位元組字元,則會傳回 -1。

備註

如果 不是 NULL ,函式會將 mbtowccount 所指向 mbcharmbchar 的位元組數轉換為對應的寬字元。 mbtowc如果 wchar 不是 NULL 則會將產生的寬字元儲存在 wchar。 mbtowc 不會檢查超過 MB_CUR_MAX 位元組。 mbtowc 會針對與地區設定相關的行為使用目前的地區設定;_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
地區設定
多位元組字元序列的解譯
_mbclen, mblen, _mblen_l
wcstombs, _wcstombs_l
wctomb, _wctomb_l