首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

wctob

在头文件<wchar.h>中定义

?

?

int wctob(wint_t c);

?

(自C95以来)

c如果其在初始转换状态下的等效多字节字符为单个字节,则缩小宽字符。

对于来自ASCII字符集的字符,这通常是可能的,因为大多数多字节编码(如UTF-8)使用单字节来编码这些字符。

参数

c

-

宽字符缩小

返回值

EOF如果c不表示长度1在初始转换状态的多字节字符。

否则,的单字节表示c作为unsigned char转化成int

示例

代码语言:javascript
复制
#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'?');
}

可能的输出:

代码语言:javascript
复制
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 ++文档

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com