[Tutor] About Python Module to Process Bytes

Steven D'Aprano steve at pearwood.info
Wed Aug 5 04:24:37 CEST 2015


On Tue, Aug 04, 2015 at 12:26:16PM -0400, Michelle Meiduo Wu wrote:

> Hi there,
> I'd like to find some python module to easily process bytes array 
> data, like encoding different types of data (char, long, short, float, 
> etc) into a same bytes array. I checked Python built-in library and 
> there are bytes and bytearray types which don't provide enough 
> functions for processing bytes easily. Does anybody know if there is 
> other convenient module available for processing bytes?

Others have suggested the struct module, but you might also mean the 
array module:

py> from array import array
py> array('B', [100, 200, 255])  # Unsigned bytes
array('B', [100, 200, 255])
py> array('B', [100, 200, 256])  # 256 overflows
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: unsigned byte integer is greater than maximum


https://docs.python.org/3/library/array.html
https://docs.python.org/2/library/array.html


-- 
Steve


More information about the Tutor mailing list