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

Previous Article
Next Article

Similar Reads

std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++
Formatting in the standard C++ libraries is done through the use of manipulators, special variables or objects that are placed on the output stream. There are two types of floating point manipulators namely, fixed floating point and scientific floating point. These all are defined in header &lt;iostream&gt;. Use of precision : In the default floati
3 min read
std::string::length, std::string::capacity, std::string::size in C++ STL
Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Header File &lt;string&gt; String Functionalities The
6 min read
std::stod, std::stof, std::stold in C++
std::stod() : It convert string into double. Syntax: double stod( const std::string&amp; str, std::size_t* pos = 0 ); double stod( const std::wstring&amp; str, std::size_t* pos = 0 ); Return Value: return a value of type double Parameters str : the string to convert pos : address of an integer to store the number of characters processed. This param
3 min read
std::setbase, std::setw , std::setfill in C++
The useful input/output manipulators are std::setbase, std::setw and std::setfill. These are defined in and are quite useful functions. std::base : Set basefield flag; Sets the base-field to one of its possible values: dec, hex or oct according to argument base.Syntax : std::setbase (int base);decimal : if base is 10hexadecimal : if base is 16octal
3 min read
Conversion of Struct data type to Hex String and vice versa
Most of the log files produced in system are either in binary(0,1) or hex(0x) formats. Sometimes you might need to map this data into readable format. Conversion of this hex information into system defined data types such as 'int/string/float' is comparatively easy. On the other hand when you have some user defined data types such as 'struct' the p
2 min read
ios manipulators oct() function in C++
The oct() method of stream manipulators in C++ is used to set the baseline format flag for the specified str stream. This flag sets the baseline for Octal numbers. Hence the output shows the number in Octal base. Syntax: ios_base&amp; oct (ios_base&amp; str) Parameters: This method accepts str as a parameter which is the stream for which the format
1 min read
ios manipulators hex() function in C++
The hex() method of stream manipulators in C++ is used to set the baseline format flag for the specified str stream. This flag sets the baseline for HexaDecimal numbers. Hence the output shows the number in HexaDecimal base. Syntax: ios_base&amp; hex (ios_base&amp; str) Parameters: This method accepts str as a parameter which is the stream for whic
1 min read
Different Ways to Convert Hex String to Integer in C++ STL
A hexadecimal number is a number whose base is 16. has numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. And 10, 11, 12, 13, 14, and 15 these numbers are denoted by A, B, C, D, E, F. In C++ STL there are certain properties that help to convert a hexadecimal string or number to a decimal number easily. There are 5 different ways to
5 min read
Convert Hex String to Signed Integer in C++
This article discusses converting a hex string to a signed integer in C++. There are 5 ways to do this in C++: Using stoi() function.Using stoul() function.Using sscanf() function.Using stringstream method.Using boost:lexical_cast.1. Using stoi function stoi is a function in the string header file. The function takes as an argument a string and ret
6 min read
How to Convert Hex String to Byte Array in C++?
A Hex String is a combination of the digits 0-9 and characters A-F and a byte array is an array used to store byte data types only. The default value of each element of the byte array is 0. In this article, we will learn how to convert a hex string to a byte array. Example: Input: Hex String : "2f4a33" Output: Byte Array : 47 74 51 Explanation: Hex
2 min read
Article Tags :
Practice Tags :