Open In App

std::oct , std::dec and std::hex in C++

Last Updated : 24 Nov, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

This function is used to set the base to octal, decimal or hexadecimal. It sets the basefield format flag for the str stream to the specified base

std::oct : When basefield is set to octal, integer values inserted into the stream are expressed in octal base (i.e., radix 8). For input streams, extracted values are also expected to be expressed in octal base when this flag is set.

std::hex : When basefield is set to hex, integer values inserted into the stream are expressed in hexadecimal base (i.e., radix 16). For input streams, extracted values are also expected to be expressed in hexadecimal base when this flag is set.

The basefield format flag can take decimal values (each with its own manipulator). This is an I/O manipulator. It may be called with an expression such as out << std::oct, std::hex or std ::dec for any out of type std::basic_ostream or with an expression
Syntax :

ios_base& hex (ios_base& str);
str :
 Stream object whose basefield format flag is affected.
 Return value :
Return the augmented string parsed in the base decimal to base octal

Examples:

Input : 
54
Output :
oct - 66
dec - 54
hex - 36




// CPP program to illustrate
// std::oct, std::hex, std::dec
#include <iostream> // std::cout, std::dec, std::hex, std::oct
  
int main()
{
    int n = 54;
    std::cout << std::oct << "oct - " << n << '\n';
    std::cout << std::dec << "dec - " << n << '\n';
    std::cout << std::hex << "hex - " << n << '\n';
    return 0;
}


Output:

oct - 66
dec - 54
hex - 36

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads