如果在初始移位狀態下等效的多字節字符為單個字節,則C++中的wctob()函數有助於將寬字符wc轉換為單個字節。由於該函數使用單字節編碼,因此用於ASCII字符集中的字符。
用法:
int wctob (wint_t wc);
參數:上麵的函數接受單個參數,如下所述:
- wc:這是wctob()函數接受的唯一參數,它是需要縮小/轉換的寬字符。
返回類型:如果該函數對應於初始狀態下單字節長度的多字節字符,則該函數返回寬字符wc的單字節表示形式。否則,它將返回EOF(文件末尾)。
以下示例程序旨在說明上述函數:
示例1:
// Program illustrating
// wctob() function
#include <bits/stdc++.h>
// function implementing the wctob() function
int fun()
{
int i, num;
const wchar_t wc[] = L"priya lal";
num = 0;
for (i = 0; i < wcslen(wc); ++i)
if (wctob(wc[i]) != EOF)
++num;
// prints the numbers of characters
// needed to be translated
wprintf(L"wc has %d characters to be translated"
"to single-byte characters.",
num);
}
// Driver Program
int main()
{
fun();
return 0;
}
輸出:
wc has 9 characters to be translatedto single-byte characters.
示例2:
// C++ program illustrating the wctob() function
#include <bits/stdc++.h>
// function implementing the wctob() function
void fun(wchar_t wc)
{
int cn = wctob(wc);
if (cn != EOF)
printf("%#x translated to %#x\n", wc, cn);
else
printf("%#x could not be translated\n", wc);
}
// Driver Program
int main(void)
{
char* utf_locale_present
= setlocale(LC_ALL, "th_TH.utf8");
// prints the result of the function
puts("In Thai UTF-8 locale:");
fun(L'a');
fun(L'?');
}
輸出:
In Thai UTF-8 locale: 0x61 translated to 0x61 0x3f translated to 0x3f
相關用法
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ real()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ valarray tan()用法及代碼示例
- C++ valarray pow()用法及代碼示例
注:本文由純淨天空篩選整理自priya_1998大神的英文原創作品 wctob() function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。