Namespaces
Variants

std:: btowc

From cppreference.net
定义于头文件 <cwchar>
std:: wint_t btowc ( int c ) ;

将单字节字符 c 扩展为其对应的宽字符形式。

大多数多字节字符编码使用单字节代码来表示ASCII字符集中的字符。此函数可用于将此类字符转换为 wchar_t

目录

参数

c - 待扩展的单字节字符

返回值

如果 c EOF ,则返回 WEOF

如果 ( unsigned char ) c 是初始移位状态下的有效单字节字符,则返回其宽字符表示,否则返回 WEOF

示例

#include <clocale>
#include <cwchar>
#include <iostream>
void try_widen(char c)
{
    std::wint_t w = std::btowc(c);
    if (w != WEOF)
        std::cout << "The single-byte character " << +(unsigned char)c
                  << " widens to " << +w << '\n';
    else
        std::cout << "The single-byte character " << +(unsigned char)c
                  << " failed to widen\n";
}
int main()
{
    std::setlocale(LC_ALL, "lt_LT.iso88594");
    std::cout << std::hex << std::showbase << "In Lithuanian ISO-8859-4 locale:\n";
    try_widen('A');
    try_widen('\xdf'); // German letter ß (U+00df) in ISO-8859-4
    try_widen('\xf9'); // Lithuanian letter ų (U+0173) in ISO-8859-4
    std::setlocale(LC_ALL, "lt_LT.utf8");
    std::cout << "In Lithuanian UTF-8 locale:\n";
    try_widen('A');
    try_widen('\xdf');
    try_widen('\xf9');
}

可能的输出:

In Lithuanian ISO-8859-4 locale:
The single-byte character 0x41 widens to 0x41
The single-byte character 0xdf widens to 0xdf
The single-byte character 0xf9 widens to 0x173
In Lithuanian UTF-8 locale:
The single-byte character 0x41 widens to 0x41
The single-byte character 0xdf failed to widen
The single-byte character 0xf9 failed to widen

参阅

在可能的情况下将宽字符窄化为单字节窄字符
(函数)
[virtual]
将字符从 char 转换为 CharT
( std::ctype<CharT> 的虚受保护成员函数)
C 文档 for btowc