std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::from_bytes
From 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) バイトシーケンスは、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 メンバ関数) | |
| 与えられた状態で、ナローマルチバイト文字列をワイド文字列に変換する (関数) | |
| [virtual] |
ファイルからの読み込み時など、ExternT から InternT への文字列を変換します。( std::codecvt<InternT,ExternT,StateT> の virtual protected メンバ関数) |