std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>:: from_bytes
|
定义于头文件
<locale>
|
||
|
wide_string from_bytes
(
char
byte
)
;
|
(1) | |
|
wide_string from_bytes
(
const
char
*
ptr
)
;
|
(2) | |
|
wide_string from_bytes
(
const
byte_string
&
str
)
;
|
(3) | |
|
wide_string from_bytes
(
const
char
*
first,
const
char
*
last
)
;
|
(4) | |
使用由
cvtptr
指向的 facet 将字节序列转换为宽字符串。
[
first
,
last
)
。
在转换开始前,若
*
this
未
通过构造函数重载
(3)
构造,则
cvtstate
将被设置为其默认值(初始转换状态)。
成功转换的输入元素数量将存储在
cvtcount
中。
目录 |
返回值
若转换成功,则返回转换结果。否则,若
*
this
是通过构造函数重载
(4)
构造的,则返回
wide_err_string
。
异常
若转换失败且 * this 未 通过构造函数重载 (4) 构造,则抛出 std::range_error 。
示例
#include <codecvt> #include <cstdint> #include <iostream> #include <locale> #include <string> int main() { std::string utf8 = "z\u00df\u6c34\U0001d10b"; // 或 u8"zß水𝄋" // 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // UTF-8 / UTF-16 标准转换 facet std::u16string utf16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(utf8.data()); std::cout << "UTF-16 转换生成了 " << utf16.size() << " 个码元: " << std::showbase; for (char16_t c : utf16) std::cout << std::hex << static_cast<std::uint16_t>(c) << ' '; // UTF-8 / UTF-32 标准转换 facet std::u32string utf32 = std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.from_bytes(utf8); std::cout << "\nUTF-32 转换生成了 " << std::dec << utf32.size() << " 个码元: "; for (char32_t c : utf32) std::cout << std::hex << static_cast<std::uint32_t>(c) << ' '; std::cout << '\n'; }
输出:
UTF-16 转换生成了 5 个码元: 0x7a 0xdf 0x6c34 0xd834 0xdd0b UTF-32 转换生成了 4 个码元: 0x7a 0xdf 0x6c34 0x1d10b
另请参阅
|
将宽字符串转换为字节字符串
(公开成员函数) |
|
|
在给定状态下将窄多字节字符串转换为宽字符串
(函数) |
|
|
[virtual]
|
将字符串从
ExternT
转换为
InternT
,例如从文件读取时
(
std::codecvt<InternT,ExternT,StateT>
的虚函数保护成员)
|