二进制
可以使用bitset来完成
#include <iostream>
#include <bitset>
using namespace std;
int main() {
int a = 3;
bitset<4>b(a);
cout << b;
}
输出结果
0011
八进制
#include <iostream>
#include <bitset>
#include <iomanip>
using namespace std;
int main() {
int a = 10;
cout << setbase(8) << a;
}
int a = 10;
cout << oct << a;
输出结果
12
十六进制
#include <iostream>
#include <bitset>
#include <iomanip>
using namespace std;
int main() {
int a = 10;
cout << hex << a << endl;
cout << uppercase << setbase(16) << a << endl;
cout << setbase(16) << a << endl;
cout << nouppercase << setbase(16) << a << endl;
}
输出结果
a
A
A
a
uppercase 十六进制数中使用 A~E。若输出前缀,则前缀输出 0X,科学计数法中输出 E
nouppercase 十六进制数中使用 a~e。若输出前缀,则前缀输出 0x,科学计数法中输出 e。