C++ provides three manipulators, dec
, oct
, and hex
, that format
numbers in different bases or radixes for output. Note the following:
hex << number
not number << hex
#include <iostream> using namespace std; int main() { cout << 2749 << endl; // decimal (base 10) output cout << dec << 2749 << endl; // decimal (base 10) output cout << oct << 2749 << endl; // octal (base 8) output cout << hex << 2749 << endl; // hexadecimal (base 16) output cout << endl; cout.setf(ios::showbase); // display output base cout << 2749 << endl; // still in hexadecimal mode cout << dec << 2749 << endl; // decimal (base 10) output cout << oct << 2749 << endl; // octal (base 8) output cout << hex << 2749 << endl; // hexadecimal (base 16) output return 0; }
Output:
2749 2749 5275 abd 0xabd 2749 05275 0xabd