命名空间
变体
操作

std::btowc

来自 cppreference.com
< cpp‎ | string‎ | multibyte
定义在头文件 <cwchar>
std::wint_t btowc( int c );

将单个字节字符 c 扩展为其宽字符等效项。

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

内容

[编辑] 参数

c - 要扩展的单个字节字符

[编辑] 返回值

如果 cEOF,则为 WEOF

如果 (unsigned char)c 是初始移位状态下的有效单字节字符,则为 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

[编辑] 另请参阅

如果可能,将宽字符缩小为单个字节窄字符
(函数) [编辑]
[虚拟]
将一个或多个字符从 char 转换为 CharT
(std::ctype<CharT> 的虚拟受保护成员函数) [编辑]
C 文档 for btowc