Difference between revisions of "cpp/string/multibyte/wctob"
From cppreference.com
m (Update links.) |
|||
Line 5: | Line 5: | ||
}} | }} | ||
− | + | 窄化宽字符 {{tt|c}} ,若其多字节字符等价版本在初始迁移状态为单字节。 | |
− | + | 这典型地对来自 ASCII 字符集的字符可行,因为大多数多字节编码(例如 UTF-8 )用单字节编码那些字符。 | |
− | === | + | ===参数=== |
{{par begin}} | {{par begin}} | ||
− | {{par | c | | + | {{par | c |要窄化的宽字符}} |
{{par end}} | {{par end}} | ||
− | === | + | ===返回值=== |
− | {{ | + | 若 {{tt|c}} 在初始迁移状态不表示长度为 {{c|1}} 的多字节字符则为 {{lc|EOF}} 。 |
− | + | 否则,为作为 {{c|unsigned char}} 的 {{tt|c}} 的单字节表示,转换为 {{c|int}} 。 | |
− | === | + | ===示例=== |
{{example | {{example | ||
| | | | ||
Line 57: | Line 57: | ||
}} | }} | ||
− | === | + | ===参阅=== |
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/string/multibyte/dsc btowc}} | {{dsc inc | cpp/string/multibyte/dsc btowc}} | ||
Line 65: | Line 65: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|en|es|fr|it|ja|pt|ru}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 01:29, 10 October 2017
Defined in header <cwchar>
|
||
int wctob( std::wint_t c ); |
||
窄化宽字符 c
,若其多字节字符等价版本在初始迁移状态为单字节。
这典型地对来自 ASCII 字符集的字符可行,因为大多数多字节编码(例如 UTF-8 )用单字节编码那些字符。
Contents |
参数
c | - | 要窄化的宽字符 |
返回值
若 c
在初始迁移状态不表示长度为 1 的多字节字符则为 EOF 。
否则,为作为 unsigned char 的 c
的单字节表示,转换为 int 。
示例
Run this code
#include <clocale> #include <cwchar> #include <iostream> void try_narrowing(wchar_t c) { int cn = std::wctob(c); if(cn != EOF) std::cout << '\'' << c << "' narrowed to " << +cn << '\n'; else std::cout << '\'' << c << "' could not be narrowed\n"; } int main() { std::setlocale(LC_ALL, "th_TH.utf8"); std::cout << std::hex << std::showbase << "In Thai UTF-8 locale:\n"; try_narrowing(L'a'); try_narrowing(L'๛'); std::setlocale(LC_ALL, "th_TH.tis620"); std::cout << "In Thai TIS-620 locale:\n"; try_narrowing(L'a'); try_narrowing(L'๛'); }
Output:
In Thai UTF-8 locale: '0x61' narrowed to 0x61 '0xe5b' could not be narrowed In Thai TIS-620 locale: '0x61' narrowed to 0x61 '0xe5b' narrowed to 0xfb
参阅
widens a single-byte narrow character to wide character, if possible (function) | |
narrows characters (public member function of std::basic_ios<CharT,Traits> )
| |
invokes do_narrow (public member function of std::ctype<CharT> )
| |
C documentation for wctob
|