Base 2 to long integers and back

Pearu Peterson pearu at cens.ioc.ee
Wed Mar 21 01:57:00 EST 2001


On Wed, 21 Mar 2001, Tim CHURCHES wrote:

> Can anyone suggest a fast method of converting base2 strings
> (e.g. '0000101110') to Python long integers, and back? I couldn't locate
> any functions in the standard library to do this, but no doubt I am
> overlooking something obvious.

Try GMPY:
	http://gmpy.sourceforge.net/

(though it is not in the standard library, it is extremely fast)

Here is an example session:
>>> from gmpy import mpz,digits
>>> mpz('0000101110',2) 
mpz(46)
>>> digits(46,2)
'101110'

Regards,
	Pearu




More information about the Python-list mailing list