共用方式為


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 傳回多位元組字元的位元組的長度。 如果 mbchar 是 NULL 或它所指向的物件是寬字元 null 字元 (L'\0'),則函式會傳回 0。 如果 mbchar 所指向的物件不會在第一個字元 count 內的有效多位元組字元,會傳回 –1。

備註

如果 mbchar 不是 NULLmbtowc 函式轉換 mbchar 指向的 count 或少量位元組至對應的寬字元。 如果 wchar 不是 NULLmbtowc 儲存產生的寬字元至 wcharmbtowc 不會檢查多餘 MB_CUR_MAX 個位元組。 mbtowc 使用目前地區設定地區相關行為; _mbtowc_l 作用相同,但是使用傳進的地區設定。 如需詳細資訊,請參閱地區設定

需求

常式

必要的標頭

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

Output

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

.NET Framework 對等用法

不適用。若要呼叫標準 C 函式,請使用 PInvoke。如需詳細資訊,請參閱平台叫用範例

請參閱

參考

資料轉換

MultiByteToWideChar

地區設定

多位元組字元序列的解譯

_mbclen、mblen、_mblen_l

wcstombs、_wcstombs_l

wctomb、_wctomb_l