From: fredrik at pythonware.com (Fredrik Lundh)
Date: Wed, 21 Apr 1999 08:26:07 GMT
Subject: Raw binary string --> long integer?
References: <7fjrar$avb$1@nnrp1.dejanews.com>
Message-ID: <002901be8bd0$ebef75b0$f29b12c2@pythonware.com>
X-UID: 1649                                                  

<ngps at my-dejanews.com> wrote:
> I'm looking for a way to do the following in pure Python.
> 
> Given a binary string, e.g., '\003\004\005', convert the string into a long.
> So the example should become (3<<8|4)<<8|5 == 110000010000000101 == 197,637.
> 
> The binary strings can be arbitrarily long. (Ok, up to about 256 bytes for an
> RSA public key. ;-)

here's one way to do it:

v = 0L
for i in map(ord, "\003\004\005"):
    v = v<<8 | i
print v

for short integers, use the struct module instead.

</F>





