title: Qt Convert Negative Hex String to Signed Integer
body: <p>I am reading the registers of an i2c device and the range of the return value is -32768 to 32768, signed integers. Below is an example:</p>
  
  <pre><code># i2cget -y 3 0x0b 0x0a w
  0xfec7
  </code></pre>
  
  <p>In Qt, I get this value ( 0xfec7 ) and want to display it in a QLabel as a signed integer. The variable stringListSplit[0] is a QString with the value '0xfec7'.</p>
  
  <pre><code>// Now update the label
  int milAmps = stringListSplit[0].toInt(0,16); // tried qint32
  qDebug() &lt;&lt; milAmps;
  </code></pre>
  
  <p>The problem is no matter what I try I always get unsigned integers, so for this example I am getting 65223 which exceeds the maximum return value specified. I need to convert the hex value to a signed integer, so I need to treat the hex value as being expressed with 2s complement. I am not seeing a simple method in the QString documentation. How can I achieve this in Qt?</p>
  
  <p>NOTE: </p>
  
  <p>QString::toShort returns 0:</p>
  
  <pre><code>// Now update the label
  short milAmps = stringListSplit[0].toShort(0,16);
  qDebug() &lt;&lt; "My new result: " &lt;&lt; milAmps;
  </code></pre>
  
  <p>For an input of stringListSplit[0] equal to '0xfebe', I get an output of -322, using the C-style casting answered by Keith like so:</p>
  
  <pre><code>// Now update the label
  int milAmps = stringListSplit[0].toInt(0,16);
  qDebug() &lt;&lt; "My new result: " &lt;&lt; (int16_t)milAmps;
  </code></pre>