Lines + structure is really too complex for this simple task
>>> x=777007543 >>> [hex(0xff&x>>8*i) for i in 3,2,1,0] ['0x2e', '0x50', '0x31', '0xb7'] >>> [hex(0xff&x>>8*i).upper() for i in 3,2,1,0] ['0X2E', '0X50', '0X31', '0XB7']
Here is a quick comparison using ipython
In [1]: import struct In [2]: x=777007543 In [3]: timeit [hex(ord(b)) for b in struct.pack('>L',x)] 100000 loops, best of 3: 2.06 us per loop In [4]: timeit [hex(0xff&x>>8*i) for i in 3,2,1,0] 1000000 loops, best of 3: 1.35 us per loop In [5]: timeit [hex(0xff&x>>i) for i in 24,16,8,0] 1000000 loops, best of 3: 1.15 us per loop
source share