title: Convert Hex Char To Int - Is there a better way?
body: <p>I have written a function to take in the data from a Sirit IDentity MaX AVI reader and parse out the facility code and keycard number. How I am currently doing it works, but is there a better way? Seems little hackish... buff &amp; buf are size 264 </p>
  
  <p><code>buf</code> and <code>buff</code> are char</p>
  
  <p>Data received from reader:</p>
  
  <blockquote>
    <p>2009/12/30 14:56:18 epc0   LN:001
    C80507A0008A19FA 0000232F Xlat'd</p>
  </blockquote>
  
  <pre><code>char TAccessReader::HexCharToInt(char n)
  {
      if (n &gt;= '0' &amp;&amp; n &lt;= '9')
          return (n-'0');
      else
  
      if (n &gt;= 'A' &amp;&amp; n &lt;= 'F')
          return (n-'A'+10);
      else
          return 0;
  }
  
  bool TAccessReader::CheckSirit(char *buf, long *key_num, unsigned char *fac) {
  
     unsigned short i, j, k;
  
     *key_num = 0; // Default is zero
     memset(buff, 0, sizeof(buff));
  
     i = sscanf(buf, "%s %s %s %s %s %s %s", &amp;buff[0], &amp;buff[20], &amp;buff[40],
                &amp;buff[60], &amp;buff[80], &amp;buff[140], &amp;buff[160]);
     if (i == 7 &amp;&amp; buff[147] &amp;&amp; !buff[148]) {
         // UUGGNNNN UU=spare, GG=Facility Code, NNNN=Keycard Number (all HEX)
  
         // get facility code
  
         *fac = HexCharToInt(buff[142]) * 16 + HexCharToInt(buff[143]);
         *key_num = (unsigned short)HexCharToInt(buff[144]) * 4096 +
                    (unsigned short)HexCharToInt(buff[145]) * 256 +
                    (unsigned short)HexCharToInt(buff[146]) * 16 +
                    HexCharToInt(buff[147]);
     }
     // do some basic checks.. return true or false
  }
  </code></pre>