Home C++ Iostream Functions setbase() C++ setbase() Manipulator Definition In C++, setbase(n) sets the number base of a stream. The allowed values are 8 (octal), 10 (decimal), and 16 (hexadecimal). It is the same as oct, dec, and hex.
The advantage is that the base can be a variable, so one line of code works for all three bases. Include <iomanip> to use it.
This is a manipulator with an argument . You write it inside a stream chain, like std::cout << setbase(...), and it changes how the stream treats the data. It lives in <iomanip>, so remember to include that header.
Syntax Put std::setbase(n) into the stream before the numbers.
#include <iomanip>
std::cout << std::setbase(16) << 255; // ff
std::cout << std::setbase(8) << 64; // 100 Call What it does std::cout << std::setbase(16) << 255Prints ff. std::cout << std::setbase(8) << 64Prints 100. std::cout << std::setbase(base) << nUses a base that is stored in a variable.
For a fixed base, hex, oct, and dec are shorter. Use setbase when the base is chosen at run time.
Library declarations /* unspecified */ setbase(int base); Parameters Parameter Description baseRequired. 8, 10, or 16. Any other value clears the base flags, and output then looks decimal.
Return Value Return Value Description Unspecified type A small helper object that sets the number base when it is sent into a stream. You never name its type. Just write setbase(base) inside the chain.
Important
Only 8, 10, and 16 work. A value like 2 does not give binary output. For binary use std::bitset or, in C++20, std::format("{:b}", n). Like hex, the base stays on the stream until changed.
C++ Examples 1. Hex 2. Loop over bases 3. Other bases Example 1 — Print in Hex with setbase The same as hex, but with a number.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setbase(16) << 255 << '\n';
std::cout << std::setbase(10) << 255 << '\n';
return 0;
} How It Works setbase(16) sets the base to hexadecimal, so 255 prints as ff.setbase(10) goes back to decimal.The second line prints the same value as 255. Example 2 — One Line for Three Bases Put the base in a variable and loop over it.
#include <iomanip>
#include <iostream>
int main() {
int bases[] = {8, 10, 16};
for (int b : bases) {
std::cout << "base " << std::dec << b << ": "
<< std::setbase(b) << 64 << '\n';
}
return 0;
} base 8: 100
base 10: 64
base 16: 40 How It Works The array holds the three allowed bases. std::setbase(b) takes the base from the loop variable, so one statement works for all three.std::dec is put in first so that the base number itself prints in decimal every time. 64 is 100 in octal, 64 in decimal, and 40 in hex.Example 3 — A Base That Is Not Supported Binary is not possible. The number simply prints as decimal.
#include <iomanip>
#include <iostream>
int main() {
std::cout << "setbase(2) prints: " << std::setbase(2) << 5 << '\n';
return 0;
} How It Works setbase(2) is not one of 8, 10, or 16, so it does not turn on binary.The stream is left with no base flag, and output with no flag is printed as decimal. The number 5 is printed as 5 and not as 101. For binary output use std::bitset. Frequently Asked Questions What does setbase() do in C++? It sets the number base of a stream to 8, 10, or 16, like oct, dec, and hex.
What is the difference between setbase and hex? They give the same result. setbase(16) takes the base as a number, so it can come from a variable.
Can setbase print binary numbers? No. Only 8, 10, and 16 are supported. Use std::bitset for binary.
Does setbase need <iomanip>? Yes. Include <iomanip> to use it.
What happens with other values? The base flags are cleared. Output then shows decimal numbers, and input accepts a 0 or 0x prefix to choose the base.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion