std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::to_bytes
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <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) | |
構築時に供給された codecvt ファセットを用いて、ワイドからマルチバイトへの変換を行います。
1) 長さ
1 の文字列であるかのように、 wchar を byte_string に変換します。2)
wptr の指すワイド文字から始まるヌル終端ワイド文字シーケンスを byte_string に変換します。3) ワイド文字列
str を byte_string に変換します。4) ワイド文字シーケンス
[first, last) を byte_string に変換します。すべての場合において、この wstring_convert のコンストラクタに非初期開始状態が提供されない限り、変換は初期シフト状態で始まります。 変換された文字数および変換状態の最終値は記憶され、 state() および converted() でアクセス可能です。
戻り値
ワイドからマルチバイトへの変換の結果を格納する byte_string オブジェクト。 変換が失敗し、この wstring_convert のコンストラクタに提供されたユーザ供給のバイトエラー文字列がある場合は、そのバイトエラー文字列を返します。
例外
この wstring_convert オブジェクトがユーザ供給のバイトエラー文字列なしで構築された場合、変換失敗時に std::range_error を投げます。
例
Run this code
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <iomanip>
// 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
関連項目
| バイト文字列をワイド文字列に変換します (パブリックメンバ関数) | |
| 指定された状態を使用してワイド文字列をマルチバイト文字列に変換します (関数) | |
[仮想] |
ファイルを書き込む時などのために、文字列を internT から externT に変換します ( std::codecvt<InternT,ExternT,State>の仮想プロテクテッドメンバ関数)
|