Best way to convert sequence of bytes to long integer

  • Follow


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(s)
487088900085839492165893L


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


Thanks in advance,


-- 
Steven
0
Reply Steven 1/20/2010 7:36:22 AM

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:
> 
>>>> makelong(s)
> 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
0
Reply Stefan 1/20/2010 7:56:48 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.
> Currently I'm using:
> 
> def makelong(s):
>     n = 0
>     for c in s:
>         n *= 256
>         n += ord(c)
>     return n
> 
> 
> which gives:
> 
>>>> makelong(s)
> 487088900085839492165893L
> 
> 
> Is this the best way, or have I missed some standard library function?

The pickle module uses

>>> import binascii
>>> s = "g%$f yg\n1\05"
>>> long(binascii.hexlify(s), 16)
487088900085839492165893L

Peter
0
Reply Peter 1/20/2010 8:43:30 AM

Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> 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)
0
Reply Paul 1/20/2010 9:10:41 AM

On Jan 20, 7:36=A0am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> I have a byte string (Python 2.x string), e.g.:
>
> s =3D "g%$f yg\n1\05"
> assert len(s) =3D=3D 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')
487088900085839492165893

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

--
Mark
0
Reply Mark 1/20/2010 9:10:47 AM

On Jan 20, 7:36=A0am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> I have a byte string (Python 2.x string), e.g.:
>
> s =3D "g%$f yg\n1\05"
> assert len(s) =3D=3D 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
0
Reply Mark 1/20/2010 2:18:55 PM

* Mark Dickinson:
> On Jan 20, 7:36 am, Steven D'Aprano <st...@REMOVE-THIS-
> 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, 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.

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
0
Reply Alf 1/20/2010 6:25:59 PM

On Wed, 20 Jan 2010 19:27:34 +0100, Alf P. Steinbach wrote:

> * Mark Dickinson:
>> On Jan 20, 7:36 am, Steven D'Aprano <st...@REMOVE-THIS-
>> 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, 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.
> 
> 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.


-- 
Steven
0
Reply Steven 1/20/2010 9:23:24 PM

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 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
0
Reply Helmut 1/21/2010 11:02:53 AM

8 Replies
565 Views

(page loaded in 0.18 seconds)

Similiar Articles:













7/23/2012 4:01:56 PM


Reply: