C++ 進制輸出

文章目錄


C++ 的進制輸出可以採用 C 語言的 printf 或者自己的 cout

printf

#include <stdio.h>

printf("%d",a); // 十進制
printf("%x",a); // 十六進制
printf("%o",a); // 八進制

cout

#include <iostream>
#include <bitset>
using namespace std;
 
cout << hex << "hex:i=" << static_cast<unsigned int>(i) << endl;
cout << oct << "oct:i=" << static_cast<unsigned int>(i) << endl;
cout << dec << "dec:i=" << static_cast<unsigned int>(i) << endl;

二進制要麻煩一點

cout << "bin:i=" << bitset<sizeof(unsigned int)*8>(i) << endl;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章