title: What do 48 and 87 values refer to when converting hexadecimal to decimal number in C?
body: <p>I am trying to understand the process of converting a hexadecimal number to its decimal equivalent, in particular when converting each hexadecimal digit to its decimal value.</p>
  
  <p>Say when the digit <code>i</code> of hexVal equals any characters ranging from '0' to '9', its decVal equals the hexVal subtracted by 48 and then timed by the digitBase:</p>
  
  <pre><code>if ((hexVal[i] &gt;= '0') &amp;&amp; (hexVal[i] &lt;= '9')) {
      decVal += (hexVal[i] - 48) * digitBase;
      ...
  }
  </code></pre>
  
  <p>I understand that 48 is ASCII value of '0'. 
  What I am in doubt with is where the values 55 and 87 come from when digit <code>i</code> of hexVal equals the ranges 'A' to 'F' and 'a' to 'f':</p>
  
  <pre><code>else if ((hexVal[i] &gt;= 'A') &amp;&amp; (hexVal[i] &lt;= 'F')) {
      hexToDec += (hexVal[i] - 55) * digitBase;
      ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>else if ((hexVal[i] &gt;= 'a') &amp;&amp; (hexVal[i] &lt;= 'f')) {
      hexToDec += (hexVal[i] - 87) * digitBase;
      ...
  }
  </code></pre>
  
  <p>The code blocks above are extracted from the following function which works well to convert hexadecimal numbers to their equivalent decimals.</p>
  
  <pre><code>int conv_hex_to_dec(char hexVal[]) {
  
      int hexToDec = 0;
      int len = strlen(hexVal);
      int digitBase = 1; 
  
      // Extract hex characters as digits from last character
      for (int i = len - 1; i &gt;= 0; i--) {
  
          if ((hexVal[i] &gt;= '0') &amp;&amp; (hexVal[i] &lt;= '9')) {
              hexToDec += (hexVal[i] - 48) * digitBase;
              digitBase = digitBase * 16;
          }
  
          else if ((hexVal[i] &gt;= 'A') &amp;&amp; (hexVal[i] &lt;= 'F')) {
              hexToDec += (hexVal[i] - 55) * digitBase; 
              digitBase = digitBase * 16;
          }
          else if ((hexVal[i] &gt;= 'a') &amp;&amp; (hexVal[i] &lt;= 'f')) {
              hexToDec += (hexVal[i] - 87) * digitBase; 
              digitBase = digitBase * 16;
          }
          else {
              printf("Invalid hex val");
          }
      }
  
      return hexToDec;
  }
  </code></pre>
  
  <p>Any explanation will be much appreciated.</p>
  
  <p>Thanks.</p>