wctomb ()- 将宽字符转换为多字节字符

格式

#include <stdlib.h>
int wctomb(char *string, wchar_t character);

语言级别

ANSI

线程安全

False

请改为使用 wcrtomb()

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 如果在编译命令中指定了 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF) ,那么此行为也可能受当前语言环境的 LC_UNI_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

宽字符函数

有关更多信息,请参阅 宽字符

描述

wctomb() 函数将 characterwchar_t 值转换为由 string指向的多字节数组。 如果 字符 的值为 0,那么该函数将处于初始移位状态。 wctomb() 函数最多将 MB_CUR_MAX 字符存储在 string中。

宽字符的转换与 wcstombs()中描述的相同。 请参阅此函数以获取 Unicode 示例。

返回值

wctomb() 函数返回多字节字符的长度 (以字节计)。 如果 字符不是有效的多字节字符,将返回值 -1 。 如果 stringNULL 指针,那么如果使用了依赖于 shift 的编码,那么 wctomb() 函数将返回非零值,否则返回 0

如果发生转换错误,那么可以将 errno 设置为 ECONVERT

示例

此示例将宽字符 c 转换为多字节字符。
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
 
#define SIZE 40
 
int main(void)
{
  static char  buffer[ SIZE ];
  wchar_t wch = L'c';
  int length;
 
  length = wctomb( buffer, wch );
  printf( "The number of bytes that comprise the multibyte "
             "character is %i\n", length );
  printf( "And the converted string is \"%s\"\n", buffer );
}
 
/****************  Output should be similar to:  ******************
 
The number of bytes that comprise the multibyte character is 1
And the converted string is "c"
*/

相关信息