std::wctob
来自cppreference.com
| 在标头 <cwchar> 定义
|
||
| |
||
窄化宽字符 c,若其多字节字符等价版本在初始迁移状态为单字节。
这典型地对来自 ASCII 字符集的字符可行,因为大多数多字节编码(例如 UTF-8)用单字节编码那些字符。
参数
| c | - | 要窄化的宽字符 |
返回值
若 c 在初始迁移状态不表示长度为 1 的多字节字符则为 EOF。
否则,为作为 unsigned char 的 c 的单字节表示,转换为 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) << "' 窄化为 " << +cn << '\n';
else
std::cout << '\'' << int(c) << "' 无法窄化\n";
}
int main()
{
std::setlocale(LC_ALL, "th_TH.utf8");
std::cout << std::hex << std::showbase << "在泰语 UTF-8 本地环境中:\n";
try_narrowing(L'a');
try_narrowing(L'๛');
std::setlocale(LC_ALL, "th_TH.tis620");
std::cout << "在泰语 TIS-620 本地环境中:\n";
try_narrowing(L'a');
try_narrowing(L'๛');
}
输出:
在泰语 UTF-8 本地环境中:
'0x61' 窄化为 0x61
'0xe5b' 无法窄化
在泰语 TIS-620 本地环境中:
'0x61' 窄化为 0x61
'0xe5b' 窄化为 0xfb
