Is there some equivalent function in python for pack from php?
Question: I'm not familiar with binary operations in general, so for PHP i did it by
examples, but can't find anything for python... I need something in python
like in php `pack('H*'` and `pack('a*'`.


Answer: The python [`struct`](https://docs.python.org/2/library/struct.html%29) module
is what you're looking for.

    
    
    >>> import struct
    >>> struct.pack("<H", 5) # Little endian short, 2 bytes.
    '\x05\x00'
    

Do note that [`php.pack()`](http://www.php.net/manual/en/function.pack.php)
`H` is not the same as struct `H`.

If you want to convert an integer to a hexadecimal:

    
    
    >>> import random
    >>> hex(random.randint(0, 100))
    '0x4c'
    

but in python hexadecimals are strings, so I'm not sure if that's what you're
asking. If you want to actually _pack_ the output of `random.randint()` as
binary data, pack the value as some integer. You can use the format characters
`B, H, I, L, Q` at least.

