title: python pack 4 byte integer with bytes in bytearray struct.pack
body: <p>I am attempting to pack the contents of a python bytearray into a 4byte signed integer using struct.pack.  Unfortunately, pack wants a string, so after some googling I figured I needed to decode my bytearray to a string.  I figured ascii meant since because an ascii character is a byte long.  Unfortunately, ascii did not want to support my values > 127, so I thought I would use replace...</p>
  
  <p>but when I do this decode returns an object of type unicode and now each of my bytes is a 4 character string...</p>
  
  <p>This just seems a little ridiculous, im missing something obvious (ps I have been using python for about two weeks)</p>
  
  <p>here is what I am trying to do...</p>
  
  <pre><code>    val = long(-5) 
  s = bytearray(pack("&lt;i", val)) 
  
  s.pop() # pop off msb
  
  # write it out the way we want to then read it in the way the code does
  fout = open("test.bat", "wb")
  fout.write(s) 
  fout.close()
  
  fin = open("test.bat", "rb") 
  
  inBytes = bytearray(fin.read(3))
  # extend sign bit
  if (inBytes[2] &amp; 0x80):
      inBytes.append(0xff)
  else:
      inBytes.append(0x00)
  
  nb = inBytes.decode('ascii', 'replace')
  # ERROR:root:after decode, len: 4 type: &lt;type 'unicode'&gt;
  logging.error("after decode, len: {0} type: {1}".format(len(nb), type(nb)))
  
  # struct.error: unpack requires a string argument of length 4 
  inInt32 = unpack('&lt;i', inBytes.decode('ascii', 'replace'))[0]
  
  fin.close()
  </code></pre>