title: c++ stream negative number conversion
body: <p>I came across an issue with C++ trying to read a text file filled with signed integer numbers in hexadecimal form and parsing them to vectors. I used the C++ stream to variable redirect (stream >> var), and it seems that negative numbers are not parsed correctly - the variable gets the value 0, and the stream fail flag is set.</p>
  
  <p>If I try to convert the string using strtol() function, the results are as expected. Likewise, if I try to first redirect the stream to an unsigned integer and than cast the variable to signed integer, the results are again correct and no stream error is reported.</p>
  
  <p>I'm using gcc 6.3.0 on Debian 9.1 (x64), running on Xeon E5-2643 v3 system.</p>
  
  <p>Did anyone else experience this issue? I would expect the conversion to work the same way as the strtol function, and not report any stream errors. Am I missing some stream settings / forgetting to call some function or set some flag here?</p>
  
  <p>Any suggestions would be greatly appreciated.</p>
  
  <p>Attached below is an example C++ program demonstrating this problem.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstdint&gt;
  
  
  int main()
  {
    const char* minus_one = "0xffffffff";
  
    std::stringstream ss;
    ss &lt;&lt; minus_one;
  
    std::cout &lt;&lt; "input string    : " &lt;&lt; ss.str() &lt;&lt; "\n"; // outputs "0xffffffff"
  
    // C-style conversion
    int32_t cint;
    cint = strtol(ss.str().c_str(), NULL, 0);
    std::cout &lt;&lt; "strtol conv     : " &lt;&lt; cint &lt;&lt;  " (" &lt;&lt; std::hex &lt;&lt; cint &lt;&lt; ")\n"; // outputs "-1 (ffffffff)"
    std::cout &lt;&lt; std::dec;
  
    // C++-style conversion
    int32_t cppint;
    ss &gt;&gt; std::hex &gt;&gt; cppint;
    std::cout &lt;&lt; std::dec &lt;&lt; "ssextr conv     : " &lt;&lt; cppint &lt;&lt;  " (" &lt;&lt; std::hex &lt;&lt; cppint &lt;&lt; ")\n"; // outputs "0 (0)" &lt;- ERROR
    std::cout &lt;&lt; std::dec;
    if (ss.fail()) std::cout &lt;&lt; "Error converting number.\n";
  
    // C++-style conversion with cast
    uint32_t cppuint;
    int32_t cppint2;
    ss.clear();
    ss.str(minus_one);
    ss &gt;&gt; std::hex &gt;&gt; cppuint;
    cppint2 = (int32_t)cppuint;
    std::cout &lt;&lt; std::dec &lt;&lt; "ssextr cast conv: " &lt;&lt; cppint2 &lt;&lt;  " (" &lt;&lt; std::hex &lt;&lt; cppint2 &lt;&lt; ")\n"; // outputs "-1 (0xffffffff)"
    std::cout &lt;&lt; std::dec;
    if (ss.fail()) std::cout &lt;&lt; "Error converting number.\n";
  
    exit(EXIT_SUCCESS);
  }
  </code></pre>