← Back

18. Hexadecimal Number Output

Easy

📝 Problem

Get a value and print its corresponding hexadecimal number.

Input Format:
Accept an integer as a input
Output Format:
Display the output in hexadecimal format
Constraints:
1 <= inp< =10^16
🔢 Complete Guide to Number Base Formatting in C++

Understanding Number Bases

Computers can display the same number in different bases (number systems). C++ provides powerful manipulators to control how integers are displayed.

1️⃣ Number Base Manipulators

Manipulator Base Digits Used Example (255)
std::dec 10 (Decimal) 0-9 255
std::hex 16 (Hexadecimal) 0-9, a-f ff
std::oct 8 (Octal) 0-7 377
Basic Example:
int num = 255;
cout << dec << num << endl; // 255
cout << hex << num << endl; // ff
cout << oct << num << endl; // 377
⚠️ Important: Stateful Behavior
Base manipulators are persistent — once set, they affect all future outputs until changed. Always reset if needed!

2️⃣ Hexadecimal Letter Case

  • std::uppercase → prints hex letters A–F in uppercase
  • std::nouppercase → prints letters a–f (default)
Case Example:
int num = 255;
cout << hex << num << endl;                      // ff
cout << uppercase << hex << num << endl;      // FF

3️⃣ Showing Base Prefix

  • std::showbase → adds prefix (0x for hex, 0 for octal)
  • std::noshowbase → disables prefix (default)
Prefix Examples:
int num = 255;
cout << showbase << hex << num << endl;               // 0xff
cout << uppercase << hex << num << endl;      // 0XFF
cout << oct << num << endl;                      // 0377

4️⃣ Field Width & Padding

Manipulator Purpose Duration
setw(n) Set width to n characters Next output only
setfill(c) Fill character for padding Persistent
Width & Padding:
int num = 255;
cout << setw(6) << setfill('0') << hex << num;
// Output: 0000ff

5️⃣ Handling Negative Numbers

Negative numbers show a minus sign in hex/octal by default:

Negative Numbers:
int n = -255;
cout << hex << n;  // -ff

// Two's complement (unsigned style):
cout << hex << static_cast<unsigned int>(n);
// Output: ffffff01

6️⃣ Complete Example - All Together

int num;
cin >> num;

// Decimal
cout << "Decimal: " << dec << num << '\n';

// Hex lowercase
cout << "Hex: " << hex << nouppercase << num << '\n';

// Hex uppercase with 0x prefix
cout << "Hex (0X): " << showbase << uppercase << num << '\n';

// Octal with prefix
cout << "Octal: " << showbase << oct << num << '\n';

// Padded hex
cout << setw(6) << setfill('0') << hex << num;

// Reset to default
cout << dec << noshowbase << nouppercase;

✅ Key Tips Beginners Often Miss

  1. Hex stays active until changed → always reset if needed
  2. showbase + uppercase = 0XFF (not just FF)
  3. setw() only affects next value, setfill() sticks
  4. Negative numbers behave differently with signed vs unsigned cast
  5. Hex manipulators don't work for floats (unless hexfloat is used)

📊 Quick Reference

Goal Code
Lowercase hex hex << nouppercase
Uppercase hex hex << uppercase
Hex with 0x showbase << hex
Padded hex setw(n) << setfill('0') << hex
Reset to decimal dec << noshowbase

Examples

Sample Input 1:
Input:
100
Sample Output 1:
Output:
64
Explanation: 100 in decimal = 64 in hexadecimal (6×16 + 4 = 100)
Sample Input 2:
Input:
10245
Sample Output 2:
Output:
2805
Explanation: 10245₁₀ = 2805₁₆ (2×16³ + 8×16² + 0×16 + 5 = 10245)
Solution.cpp
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    long long a;
    cin >> a;
    
    // hex: Displays number in hexadecimal (base 16)
    // Uses digits 0-9 and letters a-f
    // Example: 100 → 64, 255 → ff
    cout << hex << a;
    
    return 0;
}