Re: Best way to convert sequence of bytes to long integer

2010-01-21 Thread Helmut Jarausch
On 01/20/10 22:23, Steven D'Aprano wrote: I'm writing a module that handles, I won't call it encryption, obfuscation using classical cryptographic algorithms. One of the functions needs a deterministic but unpredictable integer generated from a human-generated password or passphrase, so I'm usin

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Steven D'Aprano
On Wed, 20 Jan 2010 19:27:34 +0100, Alf P. Steinbach wrote: > * Mark Dickinson: >> On Jan 20, 7:36 am, Steven D'Aprano > cybersource.com.au> 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

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Alf P. Steinbach
* Mark Dickinson: On Jan 20, 7:36 am, Steven D'Aprano 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. By the way, are you willing to divulge what you're using this functionality f

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Mark Dickinson
On Jan 20, 7:36 am, Steven D'Aprano 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. By the way, are you willing to divulge what you're using this functionality for? When tr

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Paul Rubin
Steven D'Aprano writes: > s = "g%$f yg\n1\05" > Is this the best way, or have I missed some standard library function? It is really a shame that there is not a standard library function for this. I usually do it using hexadecimal conversion: d = int(s.encode('hex'), 16) -- http://mail.python

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Mark Dickinson
On Jan 20, 7:36 am, Steven D'Aprano 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

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Peter Otten
Steven D'Aprano 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. > Currently I'm using: > > def makelong(s): > n = 0 > for c in s: > n *= 256 > n +=

Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Stefan Behnel
Steven D'Aprano, 20.01.2010 08:36: > 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. > Currently I'm using: > > def makelong(s): > n = 0 > for c in s: > n *= 256 >

Best way to convert sequence of bytes to long integer

2010-01-19 Thread Steven D'Aprano
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. Currently I'm using: def makelong(s): n = 0 for c in s: n *= 256 n += ord(c) return n which gives: >>> makelong