std::btowc
来自 cppreference.cn
定义于头文件 <cwchar> |
||
std::wint_t btowc( int c ); |
||
将单字节字符 c 转换为其宽字符等价形式。
大多数多字节字符编码使用单字节代码来表示来自 ASCII 字符集的字符。此函数可用于将此类字符转换为 wchar_t。
内容 |
[edit] 参数
c | - | 要拓宽的单字节字符 |
[edit] 返回值
WEOF 如果 c 是 EOF。
如果 (unsigned char)c 是初始移位状态下的有效单字节字符,则为 c 的宽字符表示形式;否则为 WEOF。
[edit] 示例
运行此代码
#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
[edit] 参见
如果可能,将宽字符窄化为单字节窄字符 (函数) | |
[virtual] |
将一个或多个字符从 char 转换为 CharT (std::ctype<CharT> 的虚保护成员函数) |
C 文档 for btowc
|