Namespaces
Variants

std:: dec, std:: hex, std:: oct

From cppreference.net
< cpp ‎ | io ‎ | manip
Input/output manipulators
Floating-point formatting
Integer formatting
dec hex oct
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
헤더 파일에 정의됨 <ios>
(1)
(2)
(3)

정수 I/O에 대한 기본 숫자 진법을 수정합니다.

1) 스트림 str basefield dec 로 설정합니다. 마치 str. setf ( std:: ios_base :: dec , std:: ios_base :: basefield ) 를 호출한 것처럼 동작합니다.
2) 스트림 str basefield hex 로 설정합니다. 마치 str. setf ( std:: ios_base :: hex , std:: ios_base :: basefield ) 를 호출한 것처럼 동작합니다.
3) 스트림 str basefield oct 로 설정합니다. 마치 str. setf ( std:: ios_base :: oct , std:: ios_base :: basefield ) 를 호출한 것처럼 동작합니다.

이것은 I/O 조정자입니다. 다음과 같은 표현식으로 호출될 수 있습니다: out << std :: hex (여기서 out std::basic_ostream 타입의 객체), 또는 다음과 같은 표현식으로 호출될 수 있습니다: in >> std :: hex (여기서 in std::basic_istream 타입의 객체).

목차

매개변수

str - I/O 스트림에 대한 참조

반환값

str (조작 후 스트림에 대한 참조).

예제

#include <bitset>
#include <iostream>
#include <sstream>
int main()
{
    std::cout << "The number 42 in octal:   " << std::oct << 42 << '\n'
              << "The number 42 in decimal: " << std::dec << 42 << '\n'
              << "The number 42 in hex:     " << std::hex << 42 << '\n';
    int n;
    std::istringstream("2A") >> std::hex >> n;
    std::cout << std::dec << "Parsing \"2A\" as hex gives " << n << '\n';
    // the output base is sticky until changed
    std::cout << std::hex << "42 as hex gives " << 42
        << " and 21 as hex gives " << 21 << '\n';
    // Note: there is no I/O manipulator that sets up a stream to print out
    // numbers in binary format (e.g. bin). If binary output is necessary
    // the std::bitset trick can be used:
    std::cout << "The number 42 in binary:  " << std::bitset<8>{42} << '\n';
}

출력:

The number 42 in octal:   52
The number 42 in decimal: 42
The number 42 in hex:     2a
Parsing "2A" as hex gives 42
42 as hex gives 2a and 21 as hex gives 15
The number 42 in binary:  00101010

참고 항목

정수 I/O에 사용되는 진법을 변경합니다
(function)
숫자 진법을 나타내는 접두사 사용 여부를 제어합니다
(function)