Numeric type conversions

Diez B. Roggisch deets at nospam.web.de
Tue Jun 17 07:44:47 EDT 2008


John Dann wrote:

> I'm new to Python and can't readily find the appropriate function for
> the following situation:
> 
> I'm reading in a byte stream from a serial port (which I've got
> working OK with pyserial) and which contains numeric data in a packed
> binary format. Much of the data content occurs as integers encoded as
> 2 consecutive bytes, ie a 2-byte integer.
> 
> I'm guess that there should be a function available whereby I can say
> something like:
> 
> My2ByteInt = ConvertToInt(ByteStream[12:13])
> 
> to take a random example of the 2 bytes occurring at positions 12 and
> 13 in the byte stream.
> 
> Can anyone point me in the right direction towards a suitable function
> please?

see the module struct in the standard-lib.

Diez



More information about the Python-list mailing list