当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 mbtowc()用法及代码示例



描述

C库函数int mbtowc(whcar_t *pwc, const char *str, size_t n)将多字节序列转换为宽字符。

声明

以下是 mbtowc() 函数的声明。

int mbtowc(whcar_t *pwc, const char *str, size_t n)

参数

  • pwc- 这是指向 wchar_t 类型对象的指针。

  • str− 这是指向 multi-byte 字符的第一个字节的指针。

  • n- 这是要检查字符长度的最大字节数。

返回值

  • 如果 str 不为 NULL,则 mbtowc() 函数返回从 str 开始的消耗字节数,如果为 0s指向空字节,失败时指向 -1。

  • 如果 str 为 NULL,则 mbtowc() 函数在编码具有非平凡移位状态时返回非零值,如果编码为无状态,则返回零。

示例

下面的例子展示了 mbtowc() 函数的用法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *str = "This is tutorialspoint.com";
   wchar_t mb[100];
   int len;
   
   len = mblen(NULL, MB_CUR_MAX); 

   mbtowc(mb, str, len*strlen(str) );
   
   wprintf(L"%ls \n", mb );   
   
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果,该结果将在 multi-byte 中,一种二进制输出。

???

相关用法


注:本文由纯净天空筛选整理自 C library function - mbtowc()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。