btowc
来自 cppreference.com
在头文件 <wchar.h> 中定义 |
||
wint_t btowc( int c ); |
(自 C95 起) | |
将单个字节字符 c
(重新解释为 unsigned char)扩展为其宽字符等效项。
大多数多字节字符编码使用单字节代码来表示来自 ASCII 字符集的字符。此函数可用于将此类字符转换为 wchar_t。
内容 |
[编辑] 参数
c | - | 要扩展的单字节字符 |
[编辑] 返回值
WEOF 如果 c
是 EOF
c
的宽字符表示,如果 (unsigned char)c 是初始移位状态下的有效单字节字符,否则为 WEOF。
[编辑] 示例
运行此代码
#include <stdio.h> #include <wchar.h> #include <locale.h> #include <assert.h> void try_widen(unsigned char c) { wint_t w = btowc(c); if(w != WEOF) printf("The single-byte character %#x widens to %#x\n", c, w); else printf("The single-byte character %#x failed to widen\n", c); } int main(void) { char *loc = setlocale(LC_ALL, "lt_LT.iso88594"); assert(loc); printf("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 setlocale(LC_ALL, "lt_LT.utf8"); printf("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