The Wayback Machine - https://web.archive.org/web/20240118173712/https://www.geeksforgeeks.org/stdoct-stddec-and-stdhex-in-cpp/
Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 24 Nov, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads