Numpy does not support integers of arbitrary bytes, and using ctypes bit fields will be more complicated than it costs.
I would suggest using a vectorized slice to convert your data to the next higher standard integer:
buf = "000000111111222222"
a = np.ndarray(len(buf), np.dtype('>i1'), buf)
e = np.zeros(len(buf) / 6, np.dtype('>i8'))
for i in range(3):
e.view(dtype='>i2')[i + 1::4] = a.view(dtype='>i2')[i::3]
[hex(x) for x in e]
source
share