Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>use <code>std::stringstream</code></p> <pre><code>unsigned int x; std::stringstream ss; ss &lt;&lt; std::hex &lt;&lt; "fffefffe"; ss &gt;&gt; x; </code></pre> <p>the following example produces <code>-65538</code> as its result:</p> <pre><code>#include &lt;sstream&gt; #include &lt;iostream&gt; int main() { unsigned int x; std::stringstream ss; ss &lt;&lt; std::hex &lt;&lt; "fffefffe"; ss &gt;&gt; x; // output it as a signed type std::cout &lt;&lt; static_cast&lt;int&gt;(x) &lt;&lt; std::endl; } </code></pre> <p><strong>EDIT:</strong> C++11 update. In the new standard, there are a few new utility functions which you can make use of! specifically, there is a family of "string to number" functions (<a href="http://en.cppreference.com/w/cpp/string/basic_string/stol" rel="noreferrer">http://en.cppreference.com/w/cpp/string/basic_string/stol</a> and <a href="http://en.cppreference.com/w/cpp/string/basic_string/stoul" rel="noreferrer">http://en.cppreference.com/w/cpp/string/basic_string/stoul</a>). These are essentially thin wrappers around C's string to number conversion functions, but know how to deal with a <code>std::string</code></p> <p>So, the simplest answer for newer code would probably look like this:</p> <pre><code>std::string s = "0xfffefffe"; unsigned int x = std::stoul(s, nullptr, 16); </code></pre> <p><strong>NOTE:</strong> Below is my original answer, which as the edit says is not a complete answer. For a functional solution. Stick the code above the line :-).</p> <hr> <p><strong>EDIT:</strong> It appears that since <code>lexical_cast&lt;&gt;</code> is defined to have stream conversion semantics. Sadly, streams don't understand the "0x" notation. So both the <code>boost::lexical_cast</code> and my hand rolled one don't deal well with hex strings. The above solution which manually sets the input stream to hex will handle it just fine.</p> <p><a href="http://www.boost.org/doc/libs/1_39_0/libs/conversion/lexical_cast.htm" rel="noreferrer">Boost has some stuff</a> to do this as well, which has some nice error checking capabilities as well. You can use it like this:</p> <pre><code>try { unsigned int x = lexical_cast&lt;int&gt;("0x0badc0de"); } catch(bad_lexical_cast &amp;) { // whatever you want to do... } </code></pre> <p>If you don't feel like using boost, here's a light version of lexical cast which does no error checking:</p> <pre><code>template&lt;typename T2, typename T1&gt; inline T2 lexical_cast(const T1 &amp;in) { T2 out; std::stringstream ss; ss &lt;&lt; in; ss &gt;&gt; out; return out; } </code></pre> <p>which you can use like this:</p> <pre><code>// though this needs the 0x prefix so it knows it is hex unsigned int x = lexical_cast&lt;unsigned int&gt;("0xdeadbeef"); </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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