std::codecvt_utf8_utf16
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <codecvt> 中定义
|
||
template< class Elem, unsigned long Maxcode = 0x10ffff, |
||
std::codecvt_utf8_utf16是一个std::codecvt方面,它封装了一个UTF-8编码的字节串和UTF-16编码的字符串之间的转换。如果
Elem
是一个32 - 位类型,将一个UTF-16代码点存储在每个32位字符的输出序列,这codecvt方面可用于读取和写入UTF-8文件,文本文件和二进制.原文:
std::codecvt_utf8_utf16 is a std::codecvt facet which encapsulates conversion between a UTF-8 encoded byte string and UTF-16 encoded character string. If
Elem
is a 32-bit type, one UTF-16 codepoint will be stored in each 32-bit character of the output sequence. 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 |
[编辑] 示例
下面的例子演示了如何读取一个UTF -8文件成UTF-16字符串的系统上使用32位wchar_t
原文:
The following example demonstrates reading a UTF-8 file into a UTF-16 string on a system with 32-bit wchar_t
#include <fstream> #include <iostream> #include <string> #include <locale> #include <codecvt> int main() { std::ofstream("text.txt") << u8"z\u6c34\U0001d10b"; std::wifstream file1("text.txt"); file1.imbue(std::locale("en_US.UTF8")); std::cout << "Normal read from file (using default UTF-8/UTF-32 codecvt)\n"; for(wchar_t c; file1 >> c; ) std::cout << std::hex << std::showbase << c << '\n'; std::wifstream file2("text.txt"); file2.imbue(std::locale(file2.getloc(), new std::codecvt_utf8_utf16<wchar_t>)); std::cout << "UTF-16 read from the same file (using codecvt_utf8_utf16)\n"; for(wchar_t c; file2 >> c; ) std::cout << std::hex << std::showbase << c << '\n'; }
输出:
Normal read from file (using default UTF-8/UTF-32 codecvt) 0x7a 0x6c34 0x1d10b UTF-16 read from the same file (using codecvt_utf8_utf16) 0x7a 0x6c34 0xd834 0xdd0b
[编辑] 另请参阅
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-8和UCS2/UCS4 原文: converts between UTF-8 and UCS2/UCS4 (类模板) |
(C++11) |
之间的转换UTF-16和UCS2/UCS4 原文: converts between UTF-16 and UCS2/UCS4 (类模板) |