wctob
From cppreference.net
|
定义于头文件
<wchar.h>
|
||
|
int
wctob
(
wint_t c
)
;
|
(C95 起) | |
如果宽字符
c
在初始移位状态下的多字节字符等效值为单字节,则将其窄化。
这对于来自ASCII字符集的字符通常是可行的,因为大多数多字节编码(如UTF-8)使用单字节来编码这些字符。
目录 |
参数
| c | - | 宽字符转窄字符 |
返回值
EOF
如果
c
在初始移位状态下不能表示长度为
1
的多字节字符。
否则,将
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