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

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
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

oct - 66
dec - 54
hex - 36

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.