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

std::wctob

Defined in header <cwchar>

int wctob( std::wint_t c );

使宽字变窄c如果其在初始移位状态下等效的多字节字符是单个字节。

这对于ASCII字符集中的字符来说通常是可能的,因为大多数多字节编码%28(如utf-8%29)使用单个字节来编码这些字符。

参数

c

-

wide character to narrow

返回值

EOF如果c不表示具有长度的多字节字符。1处于初始移位状态。

否则,cunsigned char转换成int...

二次

代码语言:javascript
复制
#include <clocale>
#include <cwchar>
#include <iostream>
 
void try_narrowing(wchar_t c)
{
    int cn = std::wctob(c);
    if(cn != EOF)
        std::cout << '\'' << c << "' narrowed to " << +cn << '\n';
    else
        std::cout << '\'' << c << "' could not be narrowed\n";
}
 
int main()
{
    std::setlocale(LC_ALL, "th_TH.utf8");
    std::cout << std::hex << std::showbase << "In Thai UTF-8 locale:\n";
    try_narrowing(L'a');
    try_narrowing(L'๛');
 
    std::setlocale(LC_ALL, "th_TH.tis620");
    std::cout << "In Thai TIS-620 locale:\n";
    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

二次

另见

btowc

widens a single-byte narrow character to wide character, if possible (function)

narrow

narrows characters (public member function of std::basic_ios)

narrow

invokes do_narrow (public member function of std::ctype)

C.wctob文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券