C

wctob

wctob

在头文件中定义
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

参考

  • C11标准(ISO/IEC 9899:2011):

另请参阅

btowc(C95)如果可能,将单字节窄字符扩展为宽字符(函数)

| 用于wctob的C ++文档