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 <iostream>
#include <cwchar>
#include <clocale>
 
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'); // ISO-8859-4 中的德文字母 ß (U+00df)
    try_widen('\xf9'); // ISO-8859-4 中的立陶宛字母 ų (U+0173)
 
    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 的虚受保护成员函数) [编辑]
btowcC 文档