Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 24 Nov, 2017
Improve Article
Save Article

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
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!