名稱空間
變體
操作

std::wctob

來自 cppreference.com
< cpp‎ | string‎ | multibyte
在標頭檔案 <cwchar> 中定義
int wctob( std::wint_t c );

將寬字元 c 窄化,如果其在初始轉換狀態下的多位元組字元等效項是單位元組。

這對於 ASCII 字元集中的字元通常是可能的,因為大多數多位元組編碼(如 UTF-8)都使用單位元組來編碼這些字元。

目錄

[編輯] 引數

c - 要窄化的寬字元

[編輯] 返回值

如果 c 在初始轉換狀態下不表示長度為 1 的多位元組字元,則返回 EOF

否則,返回 c 的單位元組表示形式(作為 unsigned char 轉換為 int)。

[編輯] 示例

#include <clocale>
#include <cwchar>
#include <iostream>
 
void try_narrowing(wchar_t c)
{
    int cn = std::wctob(c);
    if (cn != EOF)
        std::cout << '\'' << int(c) << "' narrowed to " << +cn << '\n';
    else
        std::cout << '\'' << int(c) << "' could not be narrowed\n";
}
 
int main()
{
    std::setlocale(LC_ALL, "th_TH.utf8");
    std::cout << std::hex << std::showbase << "In Thai UTF-8 locale:\n";
    try_narrowing(L'a');
    try_narrowing(L'๛');
 
    std::setlocale(LC_ALL, "th_TH.tis620");
    std::cout << "In Thai TIS-620 locale:\n";
    try_narrowing(L'a');
    try_narrowing(L'๛');
}

輸出

In Thai UTF-8 locale:
'0x61' narrowed to 0x61
'0xe5b' could not be narrowed
In Thai TIS-620 locale:
'0x61' narrowed to 0x61
'0xe5b' narrowed to 0xfb

[編輯] 參閱

如果可能,將單位元組窄字元加寬為寬字元
(函式) [編輯]
窄化字元
(std::basic_ios<CharT,Traits> 的公共成員函式) [編輯]
呼叫 do_narrow
(std::ctype<CharT> 的公共成員函式) [編輯]
C 文件 用於 wctob