2017-07-24 40 views
-1

我有一個函數與簽名什麼可以改變顯示寬度?

sc_dt::sc_uint<12> Get() 

和在輸出

[000] 
[0000] 

cerr << "[" << hex << setw(3) << setfill('0') << 0 << dec << "]\n"; 
    cerr << "[" << hex << setw(3) << setfill('0') << Get() << dec << "]\n"; 

結果(實際上SystemC的)功能爲什麼從3至4所顯示的寬度變化?

+2

這取決於什麼功能'運營商<<(STD :: ostream的和,sc_dt :: sc_uint '定義要做。 –

回答

3
#include <systemc.h> 
#include <iostream> 
#include <iomanip> 

int sc_main(int argc, char* argv[]) 
{ 
    sc_dt::sc_uint <12> my_uint = 0; 
    std::cerr << std::hex << my_uint << std::endl; 
} 

g++ test.cpp -lsystemc && ./a.out打印此:

 SystemC 2.3.1-Accellera --- Jul 24 2017 21:50:41 
     Copyright (c) 1996-2014 by all Contributors, 
     ALL RIGHTS RESERVED 
0000 

是表示四個零(16位),而不是三個(12位),因爲你可能希望它會,因爲這是如何12- SystemC中實現了一個整數。它不會被std::setw縮短,因爲它將字符的最小編號設置爲。如果有更多的字符,那麼所有的字符都會被寫入。另外值得一提的是,你的例子中的std::dec什麼都不做,因爲之後沒有打印數字。

http://www.cplusplus.com/reference/ios/ios_base/width/
http://www.cplusplus.com/reference/iomanip/setw/

這將打印僅低12位最後3個字符:

#include <systemc.h> 
#include <iostream> 
#include <iomanip> 

const unsigned CHARS = 3; 
const unsigned MASK = (1u << CHARS * 4) -1; // Same as 0xFFF 

int sc_main(int argc, char* argv[]) 
{ 
    sc_dt::sc_uint <12> my_uint = 0xABC; 
    std::cerr << std::hex 
       << std::setw (CHARS) << std::setfill ('0') 
       << (my_uint & MASK) << std::endl; 
} 
+0

那你如何才能打印出最後三個字? –

+0

@DanielH我已經添加了答案。 – 2017-07-24 20:23:12

+0

通過將其轉換爲'unsigned int'而不是'sc_dt :: sc_uint'工作? –

相關問題