Best way to convert sequence of bytes to long integer

Mark Dickinson dickinsm at
Wed Jan 20 10:10:47 CET 2010

On Jan 20, 7:36 am, Steven D'Aprano <st... at REMOVE-THIS-> wrote:
> I have a byte string (Python 2.x string), e.g.:
> s = "g%$f yg\n1\05"
> assert len(s) == 10
> I wish to convert it to a long integer, treating it as base-256.

Not that it helps you right now, but provided someone finds the time,
there should be an int/long method for this in Python 2.7.  It's
already implemented for Python 3.2, so it just needs backporting.

Python 3.2a0 (py3k:77612, Jan 20 2010, 09:04:15)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int.from_bytes(b"g%$f yg\n1\05", 'big')

Until then, Peter Otten's solution is about as close as you can get, I


More information about the Python-list mailing list