C++中使用cout以hex格式輸出方式
更新時間:2022年11月09日 09:05:31 作者:qq_36208201
這篇文章主要介紹了C++中使用cout以hex格式輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
使用cout以hex格式輸出
cout << "0x"<< hex << setiosflags(ios::uppercase) << setfill('0') << setw(2) << (int)10 << endl;其中hex設(shè)置以16進(jìn)制輸出
setiosflags(ios::uppercase)設(shè)置16進(jìn)制數(shù)大寫輸出
setiosflags各參數(shù)定義
setiosflags(ios::fixed)固定的浮點顯示setiosflags(ios::scientific)指數(shù)表示setiosflags(ios::left)左對齊setiosflags(ios::right)右對齊setiosflags(ios::skipws忽略前導(dǎo)空白setiosflags(ios::uppercase)16進(jìn)制數(shù)大寫輸出setiosflags(ios::lowercase)16進(jìn)制小寫輸出setiosflags(ios::showpoint)強制顯示小數(shù)點setiosflags(ios::showpos)強制顯示符號setfill('0')設(shè)置其他字符填充 如果輸出字符的寬度不夠 則以設(shè)置的字符輸出
setw(2)設(shè)置輸出寬度,如果寬度設(shè)置為3 則輸出0x00A
PS:
最后的強轉(zhuǎn)int:有資料說明cout << hex 只對整數(shù)有效 但是我在VS上不對數(shù)值進(jìn)行強轉(zhuǎn)也能以16進(jìn)制輸出
C++ cout的一些格式化輸出
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main(int argc,char *argv[],char *envp[])
{
cout<<1234567890<<endl;
cout<<std::setiosflags(std::ios_base::right)<<std::setw(20)<<std::setfill(' ')<<1234567890<<endl;
cout.imbue(std::locale("english"));
cout<<1234567890<<endl;
cout.unsetf(cout.flags());
cout<<std::showbase<<std::hex<<1234567890<<endl;
cout.unsetf(cout.flags());
return 0;
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
探討編寫int strlen(char *strDest);不允許定義變量的問題
本篇文章是對編寫int strlen(char *strDest);不允許定義變量的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
簡單掌握Linux系統(tǒng)中fork()函數(shù)創(chuàng)建子進(jìn)程的用法
fork()函數(shù)只能在類Unix系統(tǒng)下使用,因為需要引入unistd頭文件,這里我們就來簡單掌握Linux系統(tǒng)中fork()函數(shù)創(chuàng)建子進(jìn)程的用法,需要的朋友可以參考下2016-06-06

