(using python 3 here, there are some nomenclature differences in 2)
Well first, you could just leave everything as bytes
. This is perfectly valid:
reg_val_msb, reg_val_lsb = struct.pack('<H', 0xABCD)
bytes
allows for "tuple unpacking" (not related to struct.unpack
, tuple unpacking is used all over python). And bytes
is an array of bytes, which can be accessed via index as you wanted.
b = struct.pack('<H',0xABCD)
b[0],b[1]
Out[52]: (205, 171)
If you truly wanted to get it into an array.array('B')
, it's still rather easy:
ary = array('B',struct.pack('<H',0xABCD))
# ary = array('B', [205, 171])
print("0x%X" % ary[0])
# 0xCD