非常教程

C参考手册

字符串 | Strings

wctob

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

参考

  • C11标准(ISO/IEC 9899:2011):
    • 7.29.6.1.2 wctob函数(p: 441)
  • C99标准(ISO/IEC 9899:1999):
    • 7.24.6.1.2 wctob函数(p:387)

另请参阅

btowc(C95)

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

| 用于wctob的C ++文档

C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。