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


C++ wctob()用法及代碼示例

C++ 中的wctob() 函數將寬字符轉換為單字節字符(char 類型),如果它的多字節字符等效是單字節。

wctob() 函數在<cwchar> 頭文件中定義。

wctob()原型

int wctob( wint_t c );

wctob() 函數將寬字符 c 作為其參數,並盡可能返回其等效的窄單字節字符。

參數:

  • c :要縮小的寬字符。

返回:

如果 c 表示初始移位狀態下長度為 1 的多字節字符,則 wctob() 函數返回 c 的單字節表示。否則返回 EOF。

示例:wctob() 函數如何工作?

#include <cwchar>
#include <clocale>
#include <iostream>
#include <cstdio>
using namespace std;

void test_wctob(wchar_t c)
{
	int ch = wctob(c);
	if (ch != EOF)
		wcout << c << L" can be narrowed" << endl;
	else
		wcout << c << L" can't be narrowed" << endl;
}

int main()
{
	setlocale(LC_ALL, "en_US.utf8");

	wchar_t wc1 = L'm';
	wchar_t wc2 = L'\u00c6';
	
	test_wctob(wc1);
	test_wctob(wc2);
	
	return 0;
}

運行程序時,輸出將是:

m can be narrowed
Æ can't be narrowed

相關用法


注:本文由純淨天空篩選整理自 C++ wctob()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。