title: Convert single Character (hex number) to Integer in C
body: <p>So for an assignment I have to convert a character (<code>0</code>-<code>F</code>) to an integer (<code>0</code>-<code>15</code>), <code>0</code>-<code>9</code> works fine, but if any letter is given, it prints a random number: For <code>C</code> for instance, it gives <code>19</code>, for <code>D</code> is returns <code>20</code>.</p>
  
  <p>This is my method:</p>
  
  <pre><code>int char2int(char digit) {
      int i = 0;
  
      if (digit == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9)
          i = digit - '0';
      else
      if (digit == 'A' || 'B' || 'C' || 'D' || 'E' || 'F')
          i = digit - '9';
      else
          i = -1;
  
      return i;
  }
  </code></pre>
  
  <p>At first my if statements were like this:</p>
  
  <pre><code>if (digit =&gt; 0 &amp;&amp; =&lt; 9)
  
  if (digit =&gt; A &amp;&amp; =&lt; F)
  </code></pre>
  
  <p>But that gave a number of errors. You can tell I don't know C very well. My  current If statement works but I'm sure it's unnecessarily long.</p>