How to convert bytearray into integer?

Mark Dickinson dickinsm at gmail.com
Mon Aug 16 21:53:21 CEST 2010


On Aug 16, 8:36 pm, Mark Dickinson <dicki... at gmail.com> wrote:
> On Aug 16, 8:08 pm, Jacky <jacky.chao.w... at gmail.com> wrote:
> > My concern is that struct may need to parse the format string,
> > construct the list, and de-reference index=0 for this generated list
> > to get the int out.
>
> > There should be some way more efficient?
>
> Well, you can improve on the struct solution by using the
> struct.Struct class to avoid parsing the format string repeatedly:
>
> >>> import struct
> >>> S = struct.Struct('<I')
> >>> S.unpack_from(buffer(bytearray([1,2,3,4,5])))
>
> (67305985,)
>
> This doesn't make a huge difference on my machine (OS X 10.6.4, 64-bit
> build of Python 2.6) though;  it's probably more effective for long
> format strings.

Sorry, this was inaccurate:  this makes almost *no* significant
difference on my machine for large test runs (10000 and up).  For
small ones, though, it's faster.  The reason is that the struct module
caches (up to 100, in the current implementation) previously used
format strings, so with your tests you're only ever parsing the format
string once anyway.  Internally, the struct module converts that
format string to a Struct object, and squirrels that Struct object
away into its cache, which is implemented as a dict from format
strings to Struct objects.  So the next time that the format string is
used it's simply looked up in the cache, and the Struct object
retrieved.

By the way, in Python 3.2 there's yet another fun way to do this,
using int.from_bytes.

>>> int.from_bytes(bytearray([1,2,3,4]), 'little')
67305985

--
Mark



More information about the Python-list mailing list