title: Convert 4 bytes to integer using little-endian format (least signficant byte first)
body: <p>According to the <a href="http://en.wikipedia.org/wiki/BMP_file_format" rel="nofollow">Wikipedia</a> page, the width of a <code>.bmp</code> image is stored in the header of the file, in bytes 0x12 through 0x15. For example, in an image that is 256x256, bytes 0x12 through 0x15 would look like this; Ruby converts each byte to an integer:</p>
  
  <pre><code>file = File.open("red_bmp.bmp", "r")
  bytes = file.bytes.to_a
  bytes[0x12..0x15]
  #=&gt; [0, 1, 0, 0]
  </code></pre>
  
  <p>In order to convert this to little-endian format, my best solution is to convert each decimal value to a hex string, reverse the array, join the elements, and convert the resulting hex string back to an integer.</p>
  
  <pre><code>width = bytes[0x12..0x15].map {|x| x.to_s(16).rjust(2, "0")}.reverse.join.to_i(16)
  #=&gt; 256
  </code></pre>
  
  <ul>
  <li>Is there a simpler way to go about this?</li>
  <li>Is there an easy way to read a file and return an array of hex values instead of integers (without resorting to <code>x.to_s(16).rjust(2, "0")</code>)?</li>
  </ul>