C++ sign extension
<p>I am working on a homework problem, printing from a binary file.
I have searched and found out that my problem is a sign extension problem.</p>

<p>In c the correct action would be to cast to an (unsigned char)</p>

<p>I have tried this solution and it does not work with cout</p>

<p>output with (unsigned) is:</p>

<pre><code>4D 5A FFFFFF90 00 03 00 00 00 04 00 00 00 FFFFFFFF FFFFFFFF 00 00 
</code></pre>

<p>output with (unsigned char) is:</p>

<pre><code>0M 0Z 0ê 0� 0 0� 0� 0� 0 0� 0� 0� 0ˇ 0ˇ 0� 0� 
</code></pre>

<p>Any guidance would be most helpful;</p>

<p>Here is the code:</p>

<pre><code>void ListHex(ifstream &amp;inFile)
{
    // declare variables
    char buf[NUMCHAR];
    unsigned char bchar;

    while(!inFile.eof())
    {
       inFile.read(buf,NUMCHAR);
       for (int count = 0; count &lt; inFile.gcount(); ++count)
       {

        cout &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; uppercase &lt;&lt; hex &lt;&lt; 
           (unsigned)buf[count] &lt;&lt; ' ';
       }
       cout &lt;&lt; '\n';
   }
}
</code></pre>
