Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Raw binary string --> long integer?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


ngps at my-dejanews

Apr 20, 1999, 11:30 PM

Post #1 of 2 (167 views)
Permalink
Raw binary string --> long integer?

Hi,

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. ;-)

Hints appreciated. Cheers.

- PS

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own


fredrik at pythonware

Apr 21, 1999, 1:26 AM

Post #2 of 2 (163 views)
Permalink
Raw binary string --> long integer? [In reply to]

<ngps [at] my-dejanews> 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>

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.