當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。