std::codecvt_utf8
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <codecvt> 中定义
|
||
template< class Elem, |
||
std::codecvt_utf8是一个std::codecvt方面,它封装了一个UTF-8编码的字节串和UCS2或UCS4字符串(取决于类型
Elem
)之间的转换。这codecvt方面可用于读取和写入UTF-8文件,文本文件和二进制. 原文:
std::codecvt_utf8 is a std::codecvt facet which encapsulates conversion between a UTF-8 encoded byte string and UCS2 or UCS4 character string (depending on the type of
Elem
). This codecvt facet can be used to read and write UTF-8 files, both text and binary. 目录 |
[编辑] 模板参数
Elem | - | 无论是char16_t,char32_t,或wchar_t
原文: either char16_t, char32_t, or wchar_t |
Maxcode | - | Elem 最大的价值,这方面没有读取或写入错误 原文: the largest value of Elem that this facet will read or write without error |
Mode | - | 恒定的类型std::codecvt_mode
原文: a constant of type std::codecvt_mode |
Inherited from std::codecvt
Member types
会员类型
|
Definition |
intern_type
|
internT
|
extern_type
|
externT
|
state_type
|
stateT
|
Member objects
会员名称
|
Type |
id (静态的)
|
std::locale::id |
Member functions
调用 do_out (公共成员函数of std::codecvt )
| |
调用 do_in (公共成员函数of std::codecvt )
| |
调用 do_unshift (公共成员函数of std::codecvt )
| |
调用 do_encoding (公共成员函数of std::codecvt )
| |
调用 do_always_noconv (公共成员函数of std::codecvt )
| |
调用 do_length (公共成员函数of std::codecvt )
| |
调用 do_max_length (公共成员函数of std::codecvt )
|
Protected member functions
[虚]</div></div>
|
转换成一个字符串,如写入文件时,从Internt的externT 原文: converts a string from internT to externT, such as when writing to file (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
将字符串转换,如从文件读取时,从externT到Internt的 原文: converts a string from externT to internT, such as when reading from file (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
产生的externT不完全转化为字符序列终止符 原文: generates the termination character sequence of externT characters for incomplete conversion (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
返回的externT需要产生一个Internt的字符的字符数,如果不变 原文: returns the number of externT characters necessary to produce one internT character, if constant (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
该方面的测试,如果所有有效参数值的标识转换为编码 原文: tests if the facet encodes an identity conversion for all valid argument values (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
计算将转换成给定的Internt的缓冲区消耗的externT字符串,该字符串的长度 原文: calculates the length of the externT string that would be consumed by conversion into given internT buffer (虚拟保护成员函数of std::codecvt )
|
[虚]</div></div>
|
返回的最大数目,可以被转换成一个单一的Internt的字符externT字符 原文: returns the maximum number of externT characters that could be converted into a single internT character (虚拟保护成员函数of std::codecvt )
|
</div>
Inherited from std::codecvt_base
会员类型
|
Definition |
enum result { ok, partial, error, noconv }; | 无作用域的枚举类型
|
枚举常量
|
Definition |
ok
|
转换完成,没有错误
原文: conversion was completed with no error |
partial
|
不是所有的源字符被转换了
原文: not all source characters were converted |
error
|
遇到无效的字符
|
noconv
|
没有转换,输入和输出类型是相同的
原文: no conversion required, input and output types are the same |
[编辑] 示例
下面的示例演示之间的差异UCS2/UTF-8和UTF-16/UTF-8转换的第三个字符的字符串不是一个有效的UCS2字符.
原文:
The following example demonstrates the difference between UCS2/UTF-8 and UTF-16/UTF-8 conversions: the third character in the string is not a valid UCS2 character.
#include <iostream> #include <string> #include <locale> #include <codecvt> int main() { // UTF-8 data. The character U+1d10b, musical sign segno, does not fit in UCS2 std::string utf8 = u8"z\u6c34\U0001d10b"; // the UTF-8 / UTF-16 standard conversion facet std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv; std::u16string utf16 = utf16conv.from_bytes(utf8); std::cout << "UTF16 conversion produced " << utf16.size() << " code points:\n"; for(char16_t c : utf16) std::cout << std::hex << std::showbase << c << '\n'; // the UTF-8 / UCS2 standard conversion facet std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv; try { std::u16string ucs2 = ucs2conv.from_bytes(utf8); } catch(const std::range_error& e) { std::u16string ucs2 = ucs2conv.from_bytes(utf8.substr(0, ucs2conv.converted())); std::cout << "UCS2 failed after producing " << std::dec << ucs2.size()<<" characters:\n"; for(char16_t c : ucs2) std::cout << std::hex << std::showbase << c << '\n'; } }
输出:
UTF16 conversion produced 4 code points: 0x7a 0x6c34 0xd834 0xdd0b UCS2 failed after producing 2 characters: 0x7a 0x6c34
[编辑] 另请参阅
Character conversions |
narrow multibyte (char) |
UTF-8 (char) |
UTF-16 (char16_t) |
---|---|---|---|
UTF-16 | mbrtoc16 / c16rtomb | codecvt<char16_t, char, mbstate_t> codecvt_utf8_utf16<char16_t> codecvt_utf8_utf16<char32_t> codecvt_utf8_utf16<wchar_t> |
N/A |
UCS2 | No | codecvt_utf8<char16_t> | codecvt_utf16<char16_t> |
UTF-32/UCS4 (char32_t) |
mbrtoc32 / c32rtomb | codecvt<char32_t, char, mbstate_t> codecvt_utf8<char32_t> |
codecvt_utf16<char32_t> |
UCS2/UCS4 (wchar_t) |
No | codecvt_utf8<wchar_t> | codecvt_utf16<wchar_t> |
wide (wchar_t) |
codecvt<wchar_t, char, mbstate_t> mbsrtowcs / wcsrtombs |
No | No |
之间进行转换的字符编码,包括UTF-8,UTF-16,UTF-32 原文: converts between character encodings, including UTF-8, UTF-16, UTF-32 (类模板) | |
(C++11) |
标签改变的的标准codecvt方面的行为 原文: tags to alter behavior of the standard codecvt facets (类) |
(C++11) |
之间的转换UTF-16和UCS2/UCS4 原文: converts between UTF-16 and UCS2/UCS4 (类模板) |
(C++11) |
converts between UTF-8 and UTF-16 (类模板) |