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

另请参阅

数据转换
区域设置
_mbclenmblen_mblen_l
mbstowcs_mbstowcs_l
mbtowc_mbtowc_l
wcstombs_wcstombs_l
WideCharToMultiByte