title: Writing hex value into file Python
body: <p>What I am really doing is creating a BMP file from JPEG using python and it's got some header data which contains info like size, height or width of the image, so basically I want to read a JPEG file, gets it width and height, calculate the new size of a BMP file and store it in the header.</p>
  <p>Let's say the new size of the BMP file is 40000 bytes whose hex value is <code>0x9c40</code>, now as there is 4 byte space to save this in the header, we can write it as <code>0x00009c40</code>. In BMP header data, LSB is written first and then MSB so I have to write, <code>0x409c0000</code> in the file.</p>
  <p>My Problems:-</p>
  <ol>
  <li><p>I was able to do this in C but I am totally lost how to do so in Python.</p>
  <p>For example, if I have <code>i=40000</code>, and by using <code>str=hex(i)[2:]</code> I got the hex value, now by some coding I was able to add the extra zeros and then reverse the code. Now how to write this <code>'409c0000'</code> data in the file as hex?</p>
  </li>
  <li><p>The header size is 54 bytes for BMP file, so is there is another way to just store the data in a string like <code>str='00ffcf4f...'</code>(upto 54 bytes) and just convert the whole str at once as hex and write it to file?</p>
  </li>
  <li><p>My friend told me to use <code>unhexlify</code> from <code>binascii</code>,
  by doing <code>unhexlify('fffcff')</code> I get <code>'\xff\xfc\xff'</code> which is what I want but  when I try <code>unhexlify('3000')</code> I get '0\x00'` which is not what I want. It is same for any value containing 3, 4, 5, 6 or 7. Is it the right way to do this?</p>
  </li>
  </ol>