Share via


wctomb, _wctomb_l

將寬字元轉換為對應的多位元組字元。 這些函式有更安全的版本可供使用;請參閱 wctomb_s_wctomb_s_l

語法

int wctomb(
   char *mbchar,
   wchar_t wchar
);
int _wctomb_l(
   char *mbchar,
   wchar_t wchar,
   _locale_t locale
);

參數

mbchar
多位元組字元的位址。

wchar
寬字元。

傳回值

如果 wctomb 將寬字元轉換成多位元組字元,它會傳回寬字元的位元組數目 (絕不會大於 MB_CUR_MAX)。 如果 wchar 是寬字元的 Null 字元 (L'\0'),則 wctomb 傳回 1。 如果目標指標 mbcharNULLwctomb 則傳回 0。 如果目前地區設定中無法轉換, wctomb 則會傳回 -1,並將 errno 設定為 EILSEQ

備註

wctomb 函式會將其 wchar 引數轉換成對應的多位元組字元,並將結果儲存在 mbchar。 您可以在任何程式的任何點呼叫函式。 wctomb 會針對任何與地區設定相關的行為使用目前的地區設定,_wctomb_lwctomb 相同,只不過它會改用傳入的地區設定。 如需詳細資訊,請參閱 Locale

wctomb 會驗證其參數。 如果 mbcharNULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行, errno 會設定為 EINVAL ,且此函式會傳回 -1。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
wctomb <stdlib.h>

如需相容性詳細資訊,請參閱相容性

範例

此程式說明 wctomb 函式的行為。

// crt_wctomb.cpp
// compile with: /W3
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   int i;
   wchar_t wc = L'a';
   char *pmb = (char *)malloc( MB_CUR_MAX );

   printf( "Convert a wide character:\n" );
   i = wctomb( pmb, wc ); // C4996
   // Note: wctomb is deprecated; consider using wctomb_s
   printf( "   Characters converted: %u\n", i );
   printf( "   Multibyte character: %.1s\n\n", pmb );
}
Convert a wide character:
   Characters converted: 1
   Multibyte character: a

另請參閱

資料轉換
地區設定
_mbclen, mblen, _mblen_l
mbstowcs, _mbstowcs_l
mbtowc, _mbtowc_l
wcstombs, _wcstombs_l
WideCharToMultiByte