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


C++ btowc()用法及代码示例


btowc()是C /C++中的内置函数,可将字符转换为等效的宽字符。它在C++的cwchar头文件中定义。

用法

wint_t btowc( int ch );

参数:该函数接受一个强制性参数ch,该参数指定要转换为宽字符的单字节字符。


返回值
如果ch是有效的单字节字符,则该函数返回ch的宽字符表示。如果ch为EOF,则返回WEOF。

以下示例程序旨在说明上述函数。

程序1:

// Program to illustrate 
// btowc() function 
#include <cstring> 
#include <cwchar> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    char str[] = "Geeksfor\xf4\xdgeeks"; 
    wchar_t wc; 
    int count = 0; 
  
    for (int i = 0; i < strlen(str); i++) { 
  
        wc = btowc(str[i]); 
        if (wc != WEOF) 
            count++; 
    } 
  
    cout << count << " out of " << strlen(str) 
         << " Characters were successfully widened"; 
    return 0; 
}
输出:
14 out of 15 Characters were successfully widened

程序2:

// Program to illustrate 
// btowc() function 
#include <cstring> 
#include <cwchar> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    char str[] = "Ishwar\xa1\xcGupta"; 
    wchar_t wc; 
    int count = 0; 
  
    for (int i = 0; i < strlen(str); i++) { 
  
        wc = btowc(str[i]); 
        if (wc != WEOF) 
            count++; 
    } 
  
    cout << count << " out of " << strlen(str) 
         << " Characters were successfully widened"; 
    return 0; 
}
输出:
12 out of 13 Characters were successfully widened


相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 btowc() function in C/C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。