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

std::c16rtomb

Defined in header <cuchar>

std::size_t c16rtomb( char* s, char16_t c16, std::mbstate_t* ps );

(since C++11)

将单个代码点从可变长度的16位字符表示形式%28(通常为utf-16%29)转换为窄的多字节字符表示形式.

如果s不是空指针,并且c16是代码点的有效可变长度编码中的最后16位代码单元,函数确定存储该代码点%28的多字节字符表示所需的字节数,包括任何移位序列%29。并将多字节字符表示存储在字符数组中,该字符数组的第一个元素由s.最多MB_CUR_MAX这个函数可以写入字节。

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

如果c16不是宽字符的16位表示形式中的最终代码单元,它不会写入所指向的数组。s,只有*ps更新。

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

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

参数

s

-

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

c16

-

the 16-bit character to convert

ps

-

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

返回值

成功后,返回字节数%28,包括写入字符数组的任何移位序列%29,该字符数组的第一个元素由s这个价值可能是​0​,例如,在处理第一个char16_t在一对代孕母亲中。

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

注记

C++标准在这个函数的语义上遵从C标准。与出版的C11不同std::mbrtoc16,它将可变宽度多字节%28(如utf-8%29)转换为可变宽度16位%28(如utf-16%29编码),此函数只能转换单个单元16位编码,这意味着它不能将utf-16转换为utf-8,尽管这是该函数的初衷。这一点已由后c11缺陷报告更正。DR 488...

二次

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <clocale>
#include <cuchar>
#include <cstdlib>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::u16string str = u"zß水?"; // or z\u00df\u6c34\U0001F34C
    std::cout << "Processing " << str.size() << " UTF-16 code units: [ ";
    for(char16_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::c16rtomb(&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 5 UTF-16 code units: [ 0x7a 0xdf 0x6c34 0xd83c 0xdf4c ]
0x7a converted to [ 0x7a ]
0xdf converted to [ 0xc3 0x9f ]
0x6c34 converted to [ 0xe6 0xb0 0xb4 ]
0xd83c converted to [ ]
0xdf4c converted to [ 0xf0 0x9f 0x8d 0x8c ]

二次

另见

mbrtoc16 (C++11)

generate the next 16-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 c16坟墓文档

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

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

扫码关注腾讯云开发者

领取腾讯云代金券