命名空间
变体
操作

std::btowc

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

[编辑] 参阅

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