Not sure if this is the most efficient way, but it looks pretty elegant:
from itertools import izip_longest
from struct import pack_into, unpack_from
def convert(bytes):
it = iter(bytes)
arr = bytearray(4)
buf = buffer(arr)
for group in izip_longest(*[it]*4, fillvalue=0):
pack_into('bbbb', arr, 0, *group)
intval, = unpack_from('<i', buf)
yield hex(intval)
char_array = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
print list(convert(char_array))
source
share