How to convert a number in Python to 8 binary bytes (64-bit long)?
Question: How do I convert a number in Python to 8 binary bytes (64-bit long)?

I have a blueprint of a network message, part of which is a number represented
with eight bytes.


Answer: Use [struct.pack](http://docs.python.org/2/library/struct.html#format-
strings):

    
    
    >>> import struct
    >>> struct.pack('!Q', 123)
    '\x00\x00\x00\x00\x00\x00\x00{'
    

The first argument is a [format
string](http://docs.python.org/2/library/struct.html#format-strings) which
controls the encoding. `!` means network byte order and `Q` is for 8-byte
unsigned integers.

