命名空间
变体
操作

std::wctob

来自 cppreference.cn
< 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