Namespaces
Variants

std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>:: to_bytes

From cppreference.net
헤더 파일에 정의됨 <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 member function)
상태 정보를 사용하여 와이드 문자열을 좁은 멀티바이트 문자열로 변환
(function)
[virtual]
InternT 에서 ExternT 로 문자열 변환 (예: 파일에 기록할 때)
( std::codecvt<InternT,ExternT,StateT> 의 virtual protected member function)