std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::from_bytes
來自 cppreference.com
< cpp | locale | wstring convert
定義於標頭檔案 <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
所指向的刻面將位元組序列轉換為寬字串。
1) 位元組序列僅包含一個元素 byte。
2) 位元組序列是始於 ptr 的空終止序列。
3) 位元組序列是包含在 str 中的序列。
4) 位元組序列是範圍
[
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"; // or u8"zß水𝄋" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // the UTF-8 / UTF-16 standard conversion facet std::u16string utf16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(utf8.data()); std::cout << "UTF-16 conversion produced " << utf16.size() << " code units: " << std::showbase; for (char16_t c : utf16) std::cout << std::hex << static_cast<std::uint16_t>(c) << ' '; // the UTF-8 / UTF-32 standard conversion facet std::u32string utf32 = std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.from_bytes(utf8); std::cout << "\nUTF-32 conversion produced " << std::dec << utf32.size() << " code units: "; for (char32_t c : utf32) std::cout << std::hex << static_cast<std::uint32_t>(c) << ' '; std::cout << '\n'; }
輸出
UTF-16 conversion produced 5 code units: 0x7a 0xdf 0x6c34 0xd834 0xdd0b UTF-32 conversion produced 4 code units: 0x7a 0xdf 0x6c34 0x1d10b
[編輯] 參閱
將寬字串轉換為位元組字串 (public member function) | |
將窄多位元組字元字串轉換為寬字串,給定狀態 (function) | |
[virtual] |
將字串從 ExternT 轉換為 InternT ,例如從檔案讀取時( std::codecvt<InternT,ExternT,StateT> 的虛保護成員函式) |