title: little endian to uint with undetermined numbers of bytes
body: <p>I was trying to write a function that took in N bytes of little endian hex and made it into an unsigned int.</p>
  
  <pre><code>unsigned int endian_to_uint(char* buf, int num_bytes)
  {
      if (num_bytes == 0)
              return (unsigned int) buf[0];
  
      return (((unsigned int) buf[num_bytes -1]) &lt;&lt; num_bytes * 8) | endian_to_uint(buf, num_bytes - 1);
  }
  </code></pre>
  
  <p>however, the value returned is approx ~256 times larger than the expected value. Why is that?</p>
  
  <p>If I needed to do use it for a 4 byte buffer, normally you'd do:</p>
  
  <pre><code>unsigned int endian_to_uint32(char* buf)
  {
      return (((unsigned int) buf[3]) &lt;&lt;   24)
           | (((unsigned int) buf[2]) &lt;&lt;   16)
           | (((unsigned int) buf[1]) &lt;&lt; 8)
           | (((unsigned int) buf[0]));
  }
  </code></pre>
  
  <p>which should be reproduced by the recursive function I wrote, or is there some arithmetic error that I haven't caught?</p>