C++ hex Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, hex sets the number base of a stream to 16 (hexadecimal). Integers are then printed with the digits 0 to 9 and a to f.

It works for both output and input, and it stays on until you change it with dec or oct. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::hex into the stream before the numbers.

C++
std::cout << std::hex << 255;       // ff
std::cin >> std::hex >> n;          // reads "ff" as 255
CallWhat it does
std::cout << std::hex << 255Prints ff.
std::cout << std::hex << std::showbase << 255Prints 0xff.
std::cin >> std::hex >> nReads a hex number such as ff or 0xff.

Add std::uppercase for capital letters like FF, and std::showbase for the 0x prefix.

Library declarations
C++
ios_base& hex(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << hex. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after hex.

C++ Examples

Example 1 — Print Numbers in Hex

See how some common values look in base 16.

C++
#include <iostream>

int main() {
    std::cout << std::hex;
    std::cout << 10 << '\n';
    std::cout << 255 << '\n';
    std::cout << 4096 << '\n';
    return 0;
}

How It Works

  1. In hex, the digits go 0, 1, ..., 9, a, ..., f. So 10 is written as a.
  2. 255 is 15 * 16 + 15, which is ff.
  3. 4096 is 16 * 16 * 16, so it is written as a one followed by three zeros: 1000.

Example 2 — Read a Hex Number

Use hex on an input stream. A leading 0x is accepted too.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("ff 0x10");
    int a = 0;
    int b = 0;

    in >> std::hex >> a >> b;

    std::cout << std::dec << a << ' ' << b << '\n';
    return 0;
}

How It Works

  1. std::hex makes >> read the text as base 16 numbers.
  2. ff becomes 255. 0x10 is read with its 0x prefix and becomes 16.
  3. The numbers are stored as plain int values. std::dec prints them in the usual form.

Example 3 — Print the Hex Code of a Character

A char must be cast to int, or it prints as a letter.

C++
#include <iostream>

int main() {
    char c = 'A';

    std::cout << std::hex;
    std::cout << c << '\n';
    std::cout << static_cast<int>(c) << '\n';
    return 0;
}

How It Works

  1. A char is printed as a character, whatever the base is. The first line is A.
  2. static_cast<int>(c) turns the letter into its code, which is 65 in decimal.
  3. In hex, 65 is 4 * 16 + 1, so the second line is 41.

Frequently Asked Questions

It sets the number base of a stream to 16, so integers are printed and read in hexadecimal.
Use std::hex and std::showbase together: cout << hex << showbase << 255.
Add std::uppercase, for example cout << hex << uppercase << 255 prints FF.
A char is printed as a letter. Cast it to int, as in static_cast<int>(c), to see the number.
Put std::dec into the stream.

Next: left

Align padded output to the left of its field.

left tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful