在线文档教程
C++
字符串 | Strings

std::mbrtoc16

性病:Mbrtoc 16

Defined in header
std::size_t mbrtoc16( char16_t* pc16, const char* s, std::size_t n, std::mbstate_t* ps (since C++11)

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

如果s不是空指针,最多检查n多字节字符串的字节,以s若要确定完成下一个多字节字符%28(包括任何移位序列%29)所需的字节数,请执行以下操作。中的下一个多字节字符。s是完整和有效的,将其转换为相应的16位字符并将其存储在*pc16%28pc16不是空%29。

中的多字节字符*s对应于多字符16[医]T序列%28等。UTF-16%29中的代理项对,然后在第一次调用此函数之后,*ps的更新方式使下一次调用mbrtoc16将写出额外的字符16[医]T,不考虑*s...

如果s为空指针,其值为npc16被忽略,并且调用等效于std::mbrtoc16(NULL, "", 1, ps)...

如果生成的宽字符为空字符,则为转换状态。*ps表示初始移位状态。

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

参数

pc16-pointer to the location where the resulting 16-bit character will be written
s-pointer to the multibyte character string used as input
n-limit on the number of bytes in s that can be examined
ps-pointer to the conversion state object used when interpreting the multibyte string

返回值

适用的第一条规定如下:

  • ​0​如果字符转换为s%28并储存在*pc16如果非空%29是空字符。

  • 字节数[1...n]成功转换的多字节字符的s

  • -3如果下一个char16_t从多个-char16_t字符%28等。代理项对%29现在已写入*pc16在这种情况下,不处理输入中的字节。

  • -2如果下一个n字节构成一个不完整的,但迄今为止有效的多字节字符。什么都没有写到*pc16...

  • -1如果出现编码错误。什么都没有写到*pc16,价值EILSEQ存储在errno的价值*ps没有具体说明。

二次

#include <iostream> #include <iomanip> #include <clocale> #include <cstring> #include <cwchar> #include <cuchar> int main() { std::setlocale(LC_ALL, "en_US.utf8" std::string str = u8"z\u00df\u6c34\U0001F34C"; // or u8"zß水?" std::cout << "Processing " << str.size() << " bytes: [ " << std::showbase; for(unsigned char c: str) std::cout << std::hex << +c << ' '; std::cout << "]\n"; std::mbstate_t state{}; // zero-initialized to initial state char16_t c16; const char *ptr = &str[0], *end = &str[0] + str.size( while(int rc = std::mbrtoc16(&c16, ptr, end - ptr, &state)) { std::cout << "Next UTF-16 char: " << std::hex << c16 << " obtained from "; if(rc == -3) std::cout << "earlier surrogate pair\n"; else if(rc > 0) { std::cout << std::dec << rc << " bytes [ "; for(int n = 0; n < rc; ++n) std::cout << std::hex << +(unsigned char)ptr[n] << ' '; std::cout << "]\n"; ptr += rc; } else break; } }

二次

产出:

二次

Processing 10 bytes: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0x7a obtained from 1 bytes [ 0x7a ] Next UTF-16 char: 0xdf obtained from 2 bytes [ 0xc3 0x9f ] Next UTF-16 char: 0x6c34 obtained from 3 bytes [ 0xe6 0xb0 0xb4 ] Next UTF-16 char: 0xd83c obtained from 4 bytes [ 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0xdf4c obtained from earlier surrogate pair

二次

另见

c16rtomb (C++11)convert a 16-bit wide character to narrow multibyte string (function)
do_in virtualconverts a string from externT to internT, such as when reading from file (virtual protected member function of std::codecvt)

c mbrtoc 16的文件

© cppreference.com

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

http://en.cpPreference.com/w/cpp/string/multibit/mbrtoc 16