from binascii import hexlify, unhexlify

def xstr(x):
    hex = '%x' % x
return unhexlify('0' * (len(hex) % 2) + hex)[::-1]

def xlong(s):
    return int(hexlify(s[::-1]), 16)
long <-> str binary conversion