Cpp - Output number in oct, hex, and dec

Introduction

Integral numbers are displayed as decimals by default.

The manipulators oct, hex, and dec can be used for switching from and to decimal display mode.

cout << hex << 11;            // Output: b 

Hexadecimals are displayed in small letters by default, that is, using a, b, ..., f.

The manipulator uppercase allows you to use capitals .

cout << hex << uppercase << 11; //Output: B 

The manipulator nouppercase returns the output format to small letters.

Enters a character and outputs its octal, decimal, and hexadecimal code.

Demo

#include <iostream>    // Declaration of cin, cout 
#include <iomanip>     // For manipulators being called 
                       // with arguments. 
#include <string> 
using namespace std; 

int main() { /* w w  w .  j a  v a  2 s.  c  om*/
    int number = 123; 
    char ch = 'A'; 
    number = ch; 

    cout << "The character " << ch 
         << " has code" << number << endl; 

    cout  << uppercase              // For hex-digits 
          << "     octal  decimal  hexadecimal\n " 
          << oct << setw(8) << number 
          << dec << setw(8) << number 
          << hex << setw(8) << number << endl; 

    return 0; 
}

Result

Related Topic