C++中使用std::cout修改数字的显示格式
std::cout 用于写入到标准输出流 。
可以让 cout 以十六进制或八进制方式显示整数。程序清单 27.1 演示了如何使用 cout 以各种格式显
示输入的数字
程序清单 27.1 使用 cout 和控制符以十进制、十六进制和八进制格式显示整数
0: #include <iostream>
1: #include <iomanip>
2: using namespace std;
3:
4: int main()
5: {
6: cout << "Enter an integer: ";
7: int input = 0;
8: cin >> input;
9:
10: cout << "Integer in octal: " << oct << input << endl;
11: cout << "Integer in hexadecimal: " << hex << input << endl;
12:
13: cout << "Integer in hex using base notation: ";
14: cout<<setiosflags(ios_base::hex|ios_base::showbase|ios_base::uppercase);
15: cout << input << endl;
16:
17: cout << "Integer after resetting I/O flags: ";
18: cout<<resetiosflags(ios_base::hex|ios_base::showbase|ios_base::uppercase);
19: cout << input << endl;
20:
21: return 0;
22: }
输出:
E