Namespaces
Variants

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

From cppreference.net
헤더 파일에 정의됨 <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"; // 또는 u8"zß水𝄋"
                 // 또는 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    // UTF-8 / UTF-16 표준 변환 패싯
    std::u16string utf16 = 
        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
                             char16_t>{}.from_bytes(utf8.data());
    std::cout << "UTF-16 변환 결과 " << utf16.size()
              << "개의 코드 단위: " << std::showbase;
    for (char16_t c : utf16)
        std::cout << std::hex << static_cast<std::uint16_t>(c) << ' ';
    // UTF-8 / UTF-32 표준 변환 패싯
    std::u32string utf32 =
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.from_bytes(utf8);
    std::cout << "\nUTF-32 변환 결과 " << std::dec
              << utf32.size() << "개의 코드 단위: ";
    for (char32_t c : utf32)
        std::cout << std::hex << static_cast<std::uint32_t>(c) << ' ';
    std::cout << '\n';
}

출력:

UTF-16 변환 결과 5개의 코드 단위: 0x7a 0xdf 0x6c34 0xd834 0xdd0b
UTF-32 변환 결과 4개의 코드 단위: 0x7a 0xdf 0x6c34 0x1d10b

참고 항목

와이드 문자열을 바이트 문자열로 변환
(public member function)
상태 정보를 사용하여 좁은 멀티바이트 문자열을 와이드 문자열로 변환
(function)
[virtual]
ExternT InternT 로 변환 (예: 파일에서 읽을 때)
( std::codecvt<InternT,ExternT,StateT> 의 virtual protected member function)