wctob

来自cppreference.com
< c‎ | string‎ | multibyte
定义于头文件 <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

[编辑] 参考

  • C11 standard (ISO/IEC 9899:2011):
  • 7.29.6.1.2 The wctob function (p: 441)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.24.6.1.2 The wctob function (p: 387)

[编辑] 参阅

(C95)
将单字节窄字符加宽成宽字符,倘若可能
(函数) [edit]
wctobC++文档