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

std::c32rtomb

Defined in header <cuchar>

std::size_t c32rtomb( char* s, char32_t c32, std::mbstate_t* ps );

(since C++11)

将UTF-32字符转换为其窄的多字节表示形式。

如果s不是空指针,函数决定存储多字节字符表示形式所需的字节数。c32%28包括任何移位序列%29,并将多字节字符表示存储在字符数组中,该字符数组的第一个元素由s.最多MB_CUR_MAX这个函数可以写入字节。

如果s为空指针,则调用等效于std::c32rtomb(buf, U'\0', ps)用于内部缓冲区buf...

如果c32是空宽字符。U'\0',则存储空字节,并在其前面加上恢复初始移位状态和转换状态参数所需的任何移位序列。*ps更新以表示初始移位状态。

此函数使用的多字节编码由当前活动的C语言环境指定。

参数

s

-

pointer to narrow character array where the multibyte character will be stored

c32

-

the 32-bit character to convert

ps

-

pointer to the conversion state object used when interpreting the multibyte string

返回值

成功后,返回字节数%28,包括写入字符数组的任何移位序列%29,该字符数组的第一个元素由s这个价值可能是​0​,例如,在处理第一个char32_t在多方面-char32_t-字符序列%28在UTF-32%29中不发生。

关于故障%28c32不是有效的32位字符%29,则返回-1、商店EILSEQerrno,还有树叶*ps处于未指定的状态。

二次

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <clocale>
#include <cuchar>
#include <cstdlib>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::u32string str = U"zß水?"; // or z\u00df\u6c34\U0001F34C
    std::cout << "Processing " << str.size() << " UTF-32 code units: [ ";
    for(char32_t c : str) std::cout << std::showbase << std::hex << c << ' ';
    std::cout << "]\n";
 
    std::mbstate_t state{};
    std::string out(MB_CUR_MAX, '\0');
    for(size_t n = 0; n < str.size(); ++n)
    {
        int rc = std::c32rtomb(&out[0], str[n], &state);
        std::cout << str[n] << " converted to [ ";
        for(int x = 0; x < rc; ++x) std::cout << +(unsigned char)out[x] << ' ';
        std::cout << "]\n";
    }
}

二次

产出:

二次

代码语言:javascript
复制
Processing 4 UTF-32 code units: [ 0x7a 0xdf 0x6c34 0x1f34c ]
0x7a converted to [ 0x7a ]
0xdf converted to [ 0xc3 0x9f ]
0x6c34 converted to [ 0xe6 0xb0 0xb4 ]
0x1f34c converted to [ 0xf0 0x9f 0x8d 0x8c ]

二次

另见

mbrtoc32 (C++11)

generate the next 32-bit wide character from a narrow multibyte string (function)

do_out virtual

converts a string from internT to externT, such as when writing to file (virtual protected member function of std::codecvt)

C文档用于c32 rgrave

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

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

扫码关注腾讯云开发者

领取腾讯云代金券