2's complement conversion. Is this right?

Peter Otten __peter__ at web.de
Mon Apr 21 23:01:14 CEST 2008


Ivan Illarionov wrote:

> And even faster:
> a = array.array('i', '\0' + '\0'.join((s[i:i+3] for i in xrange(0,
> len(s), 3))))
> if sys.byteorder == 'little':
>     a.byteswap()
> 
> I think it's a fastest possible implementation in pure python

Clever, but note that it doesn't work correctly for negative numbers. For
those you'd have to prepend "\xff" instead of "\0".

Peter



More information about the Python-list mailing list