title: Convert unsigned long int of arbitrary size to byte string
body: <p>I am looking from something to convert a large unsigned int into a byte array so that I will be able to send it over a serial port.</p>
  
  <p>In python 3.2 there is built in function <a href="https://docs.python.org/3.2/library/stdtypes.html" rel="nofollow noreferrer">to_bytes</a> to convert long integers to return a byte array. However this is not available in python 2.7. Likewise python 2.7 has a struct lib, but that library does not handle arbitrarily large ints.  There is a similar <a href="https://stackoverflow.com/questions/4670350/how-do-i-write-a-long-integer-as-binary-in-python">question</a> on stack overflow, however the accepted answer is using pickle. That would not help me as I am looking to send the long int without loosing compression. I can of course iterate over the int and come up with something custom, but I was hoping there is something available to handle the heavy lifting for me.</p>