Best way to convert sequence of bytes to long integer

S

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:
487088900085839492165893L


Is this the best way, or have I missed some standard library function?


Thanks in advance,
 
S

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
n += ord(c)
return n


which gives:

487088900085839492165893L


Is this the best way, or have I missed some standard library function?

Have you checked if the struct module offers anything here?

Stefan
 
P

Peter Otten

Steven said:
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:

487088900085839492165893L


Is this the best way, or have I missed some standard library function?

The pickle module uses
487088900085839492165893L

Peter
 
P

Paul Rubin

Steven D'Aprano said:
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)
 
M

Mark Dickinson

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.487088900085839492165893

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

Mark Dickinson

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 trying to hack out the API for int.to_bytes
and int.from_bytes on the mailing list and bug tracker, some of the
discussion about use-cases seemed a little too much like guesswork;
I'd be curious to find out about real-life use-cases.

Mark
 
A

Alf P. Steinbach

* Mark Dickinson:
By the way, are you willing to divulge what you're using this
functionality for? When trying to hack out the API for int.to_bytes
and int.from_bytes on the mailing list and bug tracker, some of the
discussion about use-cases seemed a little too much like guesswork;
I'd be curious to find out about real-life use-cases.

One possibility is that Steven wants to apply bitlevel and/or arithmetic
operations for e.g. some custom cryptographic thing. The string that he uses as
example is not completely unlike a message digest.

Another possibility is custom serialization/deserialization.

But it would be nice to know what it's actually about...



Cheers,

- Alf
 
S

Steven D'Aprano

* Mark Dickinson:

One possibility is that Steven wants to apply bitlevel and/or arithmetic
operations for e.g. some custom cryptographic thing. The string that he
uses as example is not completely unlike a message digest.

Good guess!

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 using:

hashlib.md5(key).digest()

to get a string of bytes, then converting it to an int.

int.from_bytes would be perfect for me, but in the meantime I'm using
Paul Rubin's trick of int(s.encode("hex"), 16).

Thanks to all who responded.
 
H

Helmut Jarausch

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 using:

hashlib.md5(key).digest()

to get a string of bytes, then converting it to an int.

int.from_bytes would be perfect for me, but in the meantime I'm using
Paul Rubin's trick of int(s.encode("hex"), 16).

Sorry, but this doesn't work for me (in Python 3.2a0)
since hashlib.md5(key).digest() returns a byte string
which has no .encode method.

Just my 5 cents,
Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top