wctob
来自 cppreference.cn
在头文件 <wchar.h> 中定义 |
||
int wctob( wint_t c ); |
(自 C95 起) | |
如果宽字符 c
在初始转换状态下对应的多字节字符是单字节的,则将其窄化。
这对于 ASCII 字符集中的字符通常是可能的,因为大多数多字节编码(例如 UTF-8)都使用单字节来编码这些字符。
目录 |
[编辑] 参数
c | - | 要窄化的宽字符 |
[编辑] 返回值
如果 c
不表示初始转换状态下长度为 1 的多字节字符,则返回 EOF。
否则,返回 c
的单字节表示,类型为 unsigned char 并转换为 int。
[编辑] 示例
运行此代码
#include <locale.h> #include <wchar.h> #include <stdio.h> #include <assert.h> void try_narrowing(wchar_t c) { int cn = wctob(c); if(cn != EOF) printf("%#x narrowed to %#x\n", c, cn); else printf("%#x could not be narrowed\n", c); } int main(void) { char* utf_locale_present = setlocale(LC_ALL, "th_TH.utf8"); assert(utf_locale_present); puts("In Thai UTF-8 locale:"); try_narrowing(L'a'); try_narrowing(L'๛'); char* tis_locale_present = setlocale(LC_ALL, "th_TH.tis620"); assert(tis_locale_present); puts("In Thai TIS-620 locale:"); try_narrowing(L'a'); try_narrowing(L'๛'); }
可能的输出
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