std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::to_bytes
From cppreference.com
< cpp | locale | wstring convert
| ヘッダー <locale> で定義 |
||
| byte_string to_bytes( Elem wchar ); |
(1) | |
| byte_string to_bytes( const Elem* wptr ); |
(2) | |
| byte_string to_bytes( const wide_string& wstr ); |
(3) | |
| byte_string to_bytes( const Elem* first, const Elem* last ); |
(4) | |
cvtptr によって指されるファセットを使用して、ワイドシーケンスをバイト文字列に変換します。
1) ワイドシーケンスは、単一の要素 byte のみで構成されます。
2) ワイドシーケンスは、ptr で始まるヌル終端シーケンスです。
3) ワイドシーケンスは、str に含まれるシーケンスです。
4) ワイドシーケンスは、範囲
[first, last) です。変換が開始される前に、*this がコンストラクタオーバーロード (3) で構築されなかった場合、cvtstate はデフォルト値(初期変換状態)に設定されます。
正常に変換された入力要素の数は、cvtcount に格納されます。
目次 |
[編集] 戻り値
変換が成功した場合、変換結果を返します。それ以外の場合、*this がコンストラクタオーバーロード (4) で構築された場合、byte_err_string を返します。
[編集] 例外
変換が失敗し、*this がコンストラクタオーバーロード (4) で構築されなかった場合、std::range_error がスローされます。
[編集] 例
このコードを実行
#include <codecvt> #include <iomanip> #include <iostream> #include <locale> #include <string> // utility function for output void hex_print(const std::string& s) { std::cout << std::hex << std::setfill('0'); for (unsigned char c : s) std::cout << std::setw(2) << static_cast<int>(c) << ' '; std::cout << std::dec << '\n'; } int main() { // wide character data std::wstring wstr = L"z\u00df\u6c34\U0001f34c"; // or L"zß水🍌" // wide to UTF-8 std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1; std::string u8str = conv1.to_bytes(wstr); std::cout << "UTF-8 conversion produced " << u8str.size() << " bytes:\n"; hex_print(u8str); // wide to UTF-16le std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>> conv2; std::string u16str = conv2.to_bytes(wstr); std::cout << "UTF-16le conversion produced " << u16str.size() << " bytes:\n"; hex_print(u16str); }
出力
UTF-8 conversion produced 10 bytes: 7a c3 9f e6 b0 b4 f0 9f 8d 8c UTF-16le conversion produced 10 bytes: 7a 00 df 00 34 6c 3c d8 4c df
[編集] 関連項目
| バイト文字列をワイド文字列に変換する (public メンバ関数) | |
| 与えられた状態で、ワイド文字列をナローマルチバイト文字列に変換する (関数) | |
| [virtual] |
ファイルへの書き込み時など、InternT から ExternT への文字列を変換します。( std::codecvt<InternT,ExternT,StateT> の virtual protected メンバ関数) |