Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just as a follow up, this does the same this for arbitrary width, and does the same for binary output, for example, assuming the below in in a file called hexbinfmt.h, the following:</p> <pre><code>#include &lt;hexbinfmt.h&gt; int main() { unsigned long long ll=0x7589347dfdf7834; std::cout &lt;&lt; "hexb(ll): 0x" &lt;&lt; hexb(ll) &lt;&lt; ", bin(ll): " &lt;&lt; bin(ll) &lt;&lt; '\n'; uint8_t u8t=0xce; std::cout &lt;&lt; "hexb(u8t): 0x" &lt;&lt; hexb(u8t) &lt;&lt; ", bin(u8t): " &lt;&lt; bin(u8t) &lt;&lt; '\n'; } </code></pre> <p>outputs:</p> <pre><code>hexb(ll): 0x07589347dfdf7834, bin(ll): 0000011101011000100100110100011111011111110111110111100000110100 hexb(u8t): 0xce, bin(u8t): 11001110 </code></pre> <p>Can you help me find a simpler way of doing the same thing?</p> <pre><code>#ifndef HEXBINFMT_H #define HEXBINFMT_H #include &lt;iostream&gt; #include &lt;iomanip&gt; template&lt;typename T&gt; class hexcdumper{ public: hexcdumper(T c):c(c){}; std::ostream&amp; operator()(std::ostream&amp; os) const { // set fill and width and save the previous ones to be restored later char fill=os.fill('0'); std::streamsize ss=os.width(sizeof(T)*2); // get a copy of the format flags to be restored after setting std::hex std::ios::fmtflags ff=os.flags(); // put the character out as hex if(sizeof(T)==1){ os &lt;&lt; std::hex &lt;&lt; int(c); }else{ os &lt;&lt; std::hex &lt;&lt; c; } // restore everything os.fill(fill); os.width(ss); os.flags(ff); return os; } private: T c; }; template&lt;typename T&gt; hexcdumper&lt;T&gt; hexb(T c) { return(hexcdumper&lt;T&gt;(c)); } template&lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const hexcdumper&lt;T&gt;&amp; hcd) { return(hcd(os)); } template&lt;typename T&gt; class bincdumper{ public: bincdumper(T c):c(c){}; std::ostream&amp; operator()(std::ostream&amp; os) const { T mask=1; mask &lt;&lt;= (sizeof(T)*8)-1; for(size_t i=0;i&lt;sizeof(T)*8;i++){ if(c&amp;mask){ os&lt;&lt;'1'; }else{ os&lt;&lt;'0'; } mask&gt;&gt;=1; } return os; } private: T c; }; template&lt;typename T&gt; bincdumper&lt;T&gt; bin(T c) { return(bincdumper&lt;T&gt;(c)); } template&lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const bincdumper&lt;T&gt;&amp; bcd) { return(bcd(os)); } #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload