C++輸出16進制格式

//gcgetData(int offset):  display return value in both Hex and decimal format for the data in Gantry controller area with offset address.
void getPCIData()
{
    volatile UINT32* pciaddr = (UINT32*)ptr_FPGABeam;
    int offset = pcimenu.getInt("Input Offset");
    UINT32 value = *(pciaddr+offset);
    ios::fmtflags fmtf(cout.flags());	// save old format
    std::cout << "offset=" << offset << ", value(int)=" << value << ",value(hex)=0x" << std::hex << value << std::endl;
    cout.flags(fmtf);
}

//gcsetData (int offset, int data): set the data to the Gantry Controller register at offset address.   Display the set data in both hex and decimal format.
void setPCIData()
{
    volatile UINT32* pciaddr = (UINT32*)ptr_FPGABeam;
    int offset = pcimenu.getInt("Input Offset");
    UINT32 value = pcimenu.getInt("Input Value");
    *(pciaddr+offset) = value;
    ios::fmtflags fmtf(cout.flags());	// save old format
    std::cout << "offset=" << offset << ", value(int)=" << value << ",value(hex)=0x" << std::hex << value << std::endl;
    cout.flags(fmtf);
}

如上所示,是一個獲取內存地址數據和設置內存地址數據的函數。ios

輸出16進制數據時使用到std::hex,可是有一個問題就是,輸出的數據類型只能是整數類型,如 int 或 long 類型,若是是 float 或 double 類型,輸出的時候將不會獲得正確顯示。函數

ios::fmtflags fmtf(cout.flags());用來記錄當前輸出的格式,在使用完成hex輸出以後要還原以前的輸出格式,不然後續的全部輸出都會是hex格式。code

相關文章
相關標籤/搜索